Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ejemplo.jtable;
- import java.awt.BorderLayout;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JFrame;
- import java.awt.Dimension;
- import javax.swing.JButton;
- import java.awt.Rectangle;
- import java.awt.Point;
- import java.awt.GridBagLayout;
- import javax.swing.JTextField;
- import javax.swing.JLabel;
- import com.tutorialjava.bd.ControladorBD;
- import java.awt.Font;
- import javax.swing.JScrollPane;
- import javax.swing.JTable;
- import javax.swing.ListSelectionModel;
- import javax.swing.event.ListSelectionEvent;
- import javax.swing.event.ListSelectionListener;
- import javax.swing.table.DefaultTableModel;
- public class ABMCiudadCombinado extends JFrame {
- private static final long serialVersionUID = 1L;
- private JPanel jContentPane = null;
- private JButton jPrimero = null;
- private JButton jAnterior = null;
- private JButton jSiguiente = null;
- private JButton jUltimo = null;
- private JPanel jPanel = null;
- private JPanel jPanel1 = null;
- private JTextField jCodigo = null;
- private JTextField jDescripcion = null;
- private JLabel jLabel = null;
- private JLabel jLabel1 = null;
- private JButton bNuevo = null;
- private JButton bGuardar = null;
- private JButton bModificar = null;
- private JButton bEliminar = null;
- private JButton bDeshacer = null;
- private JScrollPane jScrollPane = null;
- private JTable jTable = null;
- private CiudadesJTableModel model = new CiudadesJTableModel();
- /**
- * This is the default constructor
- */
- public ABMCiudadCombinado() {
- super();
- initialize();
- }
- /**
- * This method initializes this
- *
- * @return void
- */
- private void initialize() {
- this.setSize(650, 400);
- this.setLocation(new Point(0, 0));
- this.setContentPane(getJContentPane());
- this.setTitle("Consulta de Ciudades");
- getBDeshacer().doClick();
- }
- /**
- * This method initializes jContentPane
- *
- * @return javax.swing.JPanel
- */
- private JPanel getJContentPane() {
- if (jContentPane == null) {
- jContentPane = new JPanel();
- jContentPane.setLayout(null);
- jContentPane.add(getJPrimero(), null);
- jContentPane.add(getJAnterior(), null);
- jContentPane.add(getJSiguiente(), null);
- jContentPane.add(getJUltimo(), null);
- jContentPane.add(getJPanel(), null);
- jContentPane.add(getJPanel1(), null);
- jContentPane.add(getBNuevo(), null);
- jContentPane.add(getBGuardar(), null);
- jContentPane.add(getBModificar(), null);
- jContentPane.add(getBEliminar(), null);
- jContentPane.add(getBDeshacer(), null);
- jContentPane.add(getJScrollPane(), null);
- }
- return jContentPane;
- }
- /**
- * This method initializes jPrimero
- *
- * @return javax.swing.JButton
- */
- private JButton getJPrimero() {
- if (jPrimero == null) {
- jPrimero = new JButton();
- jPrimero.setText("<<");
- jPrimero.setSize(new Dimension(54, 25));
- jPrimero.setLocation(new Point(540, 13));
- jPrimero.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent e) {
- //System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
- String Sql = "SELECT * FROM Ciudades ORDER BY ciu_codigo LIMIT 0,1";
- try {
- ControladorBD.addSql(Sql);
- ControladorBD.execute();
- if(ControladorBD.finRecordSet()) {
- getJCodigo().setText(ControladorBD.getInt("ciu_codigo").toString() );
- getJDescripcion().setText(ControladorBD.getString("ciu_descripcion") );
- }
- actualizarJTable();
- } catch (Exception e2) {
- // TODO: handle exception
- JOptionPane.showMessageDialog(null, "Ocurrio un error en la BD" + e2.getMessage());
- e2.printStackTrace();
- }
- }
- });
- }
- return jPrimero;
- }
- /**
- * This method initializes jAnterior
- *
- * @return javax.swing.JButton
- */
- private JButton getJAnterior() {
- if (jAnterior == null) {
- jAnterior = new JButton();
- jAnterior.setText("<");
- jAnterior.setSize(new Dimension(54, 25));
- jAnterior.setLocation(new Point(540, 58));
- jAnterior.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent e) {
- //System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
- Integer cdActual = Integer.parseInt(getJCodigo().getText());
- String Sql = "SELECT * FROM Ciudades WHERE ciu_codigo < " + cdActual +
- " ORDER BY ciu_codigo DESC LIMIT 0,1";
- try {
- ControladorBD.addSql(Sql);
- ControladorBD.execute();
- if(ControladorBD.finRecordSet()) {
- getJCodigo().setText(ControladorBD.getInt("ciu_codigo").toString() );
- getJDescripcion().setText(ControladorBD.getString("ciu_descripcion") );
- }
- actualizarJTable();
- } catch (Exception e2) {
- // TODO: handle exception
- JOptionPane.showMessageDialog(null, "Ocurrio un error en la BD" + e2.getMessage());
- e2.printStackTrace();
- }
- }
- });
- }
- return jAnterior;
- }
- /**
- * This method initializes jSiguiente
- *
- * @return javax.swing.JButton
- */
- private JButton getJSiguiente() {
- if (jSiguiente == null) {
- jSiguiente = new JButton();
- jSiguiente.setText(">");
- jSiguiente.setSize(new Dimension(54, 25));
- jSiguiente.setLocation(new Point(542, 102));
- jSiguiente.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent e) {
- //System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
- Integer cdActual = Integer.parseInt(getJCodigo().getText());
- String Sql = "SELECT * FROM Ciudades WHERE ciu_codigo > " + cdActual +
- " ORDER BY ciu_codigo ASC LIMIT 0,1";
- try {
- ControladorBD.addSql(Sql);
- ControladorBD.execute();
- if(ControladorBD.finRecordSet()) {
- getJCodigo().setText(ControladorBD.getInt("ciu_codigo").toString() );
- getJDescripcion().setText(ControladorBD.getString("ciu_descripcion") );
- }
- actualizarJTable();
- } catch (Exception e2) {
- // TODO: handle exception
- JOptionPane.showMessageDialog(null, "Ocurrio un error en la BD" + e2.getMessage());
- e2.printStackTrace();
- }
- }
- });
- }
- return jSiguiente;
- }
- /**
- * This method initializes jUltimo
- *
- * @return javax.swing.JButton
- */
- private JButton getJUltimo() {
- if (jUltimo == null) {
- jUltimo = new JButton();
- jUltimo.setText(">>");
- jUltimo.setLocation(new Point(541, 148));
- jUltimo.setSize(new Dimension(54, 25));
- jUltimo.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent e) {
- System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
- Integer cdActual = Integer.parseInt(getJCodigo().getText());
- String Sql = "SELECT * FROM Ciudades WHERE ciu_codigo > " + cdActual +
- " ORDER BY ciu_codigo DESC LIMIT 0,1";
- try {
- ControladorBD.addSql(Sql);
- ControladorBD.execute();
- if(ControladorBD.finRecordSet()) {
- getJCodigo().setText(ControladorBD.getInt("ciu_codigo").toString() );
- getJDescripcion().setText(ControladorBD.getString("ciu_descripcion") );
- }
- actualizarJTable();
- } catch (Exception e2) {
- // TODO: handle exception
- JOptionPane.showMessageDialog(null, "Ocurrio un error en la BD" + e2.getMessage());
- e2.printStackTrace();
- }
- }
- });
- }
- return jUltimo;
- }
- /**
- * This method initializes jPanel
- *
- * @return javax.swing.JPanel
- */
- private JPanel getJPanel() {
- if (jPanel == null) {
- jLabel = new JLabel();
- jLabel.setBounds(new Rectangle(32, 17, 66, 15));
- jLabel.setText("Codigo");
- jPanel = new JPanel();
- jPanel.setLayout(null);
- jPanel.setLocation(new Point(173, 43));
- jPanel.setSize(new Dimension(352, 45));
- jPanel.add(getJCodigo(), null);
- jPanel.add(jLabel, null);
- }
- return jPanel;
- }
- /**
- * This method initializes jPanel1
- *
- * @return javax.swing.JPanel
- */
- private JPanel getJPanel1() {
- if (jPanel1 == null) {
- jLabel1 = new JLabel();
- jLabel1.setBounds(new Rectangle(30, 13, 77, 18));
- jLabel1.setText("Descripcion");
- jPanel1 = new JPanel();
- jPanel1.setLayout(null);
- jPanel1.setLocation(new Point(173, 100));
- jPanel1.setSize(new Dimension(352, 45));
- jPanel1.add(getJDescripcion(), null);
- jPanel1.add(jLabel1, null);
- }
- return jPanel1;
- }
- /**
- * This method initializes jCodigo
- *
- * @return javax.swing.JTextField
- */
- private JTextField getJCodigo() {
- if (jCodigo == null) {
- jCodigo = new JTextField();
- jCodigo.setLocation(new Point(121, 14));
- jCodigo.setText("0");
- jCodigo.setSize(new Dimension(62, 20));
- jCodigo.addFocusListener(new java.awt.event.FocusAdapter() {
- public void focusLost(java.awt.event.FocusEvent e) {
- System.out.println("focusLost()"); // TODO Auto-generated Event stub focusLost()
- Integer cdActual = Integer.parseInt(getJCodigo().getText());
- String Sql = "SELECT * FROM Ciudades WHERE ciu_codigo = " + cdActual +
- " ORDER BY ciu_codigo DESC LIMIT 0,1";
- try {
- ControladorBD.addSql(Sql);
- ControladorBD.execute();
- if(ControladorBD.finRecordSet()) {
- getJCodigo().setText(ControladorBD.getInt("ciu_codigo").toString() );
- getJDescripcion().setText(ControladorBD.getString("ciu_descripcion") );
- }
- actualizarJTable();
- } catch (Exception e2) {
- // TODO: handle exception
- JOptionPane.showMessageDialog(null, "Ocurrio un error en la BD" + e2.getMessage());
- e2.printStackTrace();
- }
- }
- });
- }
- return jCodigo;
- }
- /**
- * This method initializes jDescripcion
- *
- * @return javax.swing.JTextField
- */
- private JTextField getJDescripcion() {
- if (jDescripcion == null) {
- jDescripcion = new JTextField();
- jDescripcion.setSize(new Dimension(215, 20));
- jDescripcion.setEnabled(false);
- jDescripcion.setLocation(new Point(121, 16));
- }
- return jDescripcion;
- }
- /**
- * This method initializes bNuevo
- *
- * @return javax.swing.JButton
- */
- private JButton getBNuevo() {
- if (bNuevo == null) {
- bNuevo = new JButton();
- bNuevo.setLocation(new Point(13, 230));
- bNuevo.setText("Nuevo");
- bNuevo.setFont(new Font("Dialog", Font.BOLD, 10));
- bNuevo.setSize(new Dimension(97, 25));
- bNuevo.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent e) {
- System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
- LimpiarTabla();
- setEstadoEdicion(true);
- try {
- Integer ultimoCodigo = ControladorBD.maximoValorCampo("Ciudades", "ciu_codigo");
- getJCodigo().setText((ultimoCodigo + 1) + "");
- } catch (Exception e2) {
- // TODO: handle exception
- }
- getJDescripcion().setRequestFocusEnabled(true);
- }
- });
- }
- return bNuevo;
- }
- /**
- * This method initializes bGuardar
- *
- * @return javax.swing.JButton
- */
- private JButton getBGuardar() {
- if (bGuardar == null) {
- bGuardar = new JButton();
- bGuardar.setLocation(new Point(135, 230));
- bGuardar.setText("Guardar");
- bGuardar.setFont(new Font("Dialog", Font.BOLD, 10));
- bGuardar.setSize(new Dimension(97, 25));
- bGuardar.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent e) {
- //System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
- try {
- Integer cod = Integer.parseInt(getJCodigo().getText());
- Integer codBuscado = (Integer) ControladorBD.buscarCampo("Ciudades", "ciu_codigo", cod, "ciu_codigo");
- String sql = "";
- String descripcion = getJDescripcion().getText();
- if (codBuscado == null){
- sql = "INSERT INTO Ciudades (ciu_codigo,ciu_descripcion) VALUES (" + cod +", '" +descripcion + "')";
- }else {
- sql ="UPDATE Ciudades SET ciu_descripcion= '" + descripcion +"' WHERE ciu_codigo = " + cod;
- }
- ControladorBD.addSql(sql);
- ControladorBD.execute();
- getBDeshacer().doClick();
- }
- catch (Exception e2) {
- // TODO: handle exception
- JOptionPane.showMessageDialog(null, "Ocurrio un error en la BD" + e2.getMessage());
- e2.printStackTrace();
- }
- }
- });
- }
- return bGuardar;
- }
- /**
- * This method initializes bModificar
- *
- * @return javax.swing.JButton
- */
- private JButton getBModificar() {
- if (bModificar == null) {
- bModificar = new JButton();
- bModificar.setLocation(new Point(257, 230));
- bModificar.setText("Editar");
- bModificar.setSize(new Dimension(97, 25));
- bModificar.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent e) {
- //System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
- setEstadoEdicion(true);
- getJDescripcion().setRequestFocusEnabled(true);
- }
- });
- }
- return bModificar;
- }
- /**
- * This method initializes bEliminar
- *
- * @return javax.swing.JButton
- */
- private JButton getBEliminar() {
- if (bEliminar == null) {
- bEliminar = new JButton();
- bEliminar.setLocation(new Point(379, 230));
- bEliminar.setText("Borrar");
- bEliminar.setSize(new Dimension(97, 25));
- bEliminar.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent e) {
- //System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
- try {
- Integer cod = Integer.parseInt(getJCodigo().getText());
- String sql = "DELETE FROM Ciudades WHERE ciu_codigo =" + cod;
- ControladorBD.addSql(sql);
- ControladorBD.execute();
- } catch (Exception e2) {
- // TODO: handle exception
- JOptionPane.showMessageDialog(null, "Ocurrio un error en la BD" + e2.getMessage());
- e2.printStackTrace();
- }
- setEstadoEdicion(false);
- getJAnterior().doClick();
- }
- });
- }
- return bEliminar;
- }
- /**
- * This method initializes bDeshacer
- *
- * @return javax.swing.JButton
- */
- private JButton getBDeshacer() {
- if (bDeshacer == null) {
- bDeshacer = new JButton();
- bDeshacer.setLocation(new Point(501, 230));
- bDeshacer.setText("Deshacer");
- bDeshacer.setSize(new Dimension(97, 25));
- bDeshacer.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent e) {
- //System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
- setEstadoEdicion(false);
- getJAnterior().doClick();
- }
- });
- }
- return bDeshacer;
- }
- public void setEstadoEdicion(boolean isEdicion ) {
- getJCodigo().setEnabled(! isEdicion);
- getJDescripcion().setEnabled(isEdicion);
- getJPrimero().setEnabled(! isEdicion);
- getJAnterior().setEnabled(! isEdicion);
- getJSiguiente().setEnabled(! isEdicion);
- getJUltimo().setEnabled(! isEdicion);
- getBNuevo().setEnabled(! isEdicion);
- getBGuardar().setEnabled( isEdicion);
- getBModificar().setEnabled(! isEdicion);
- getBEliminar().setEnabled(! isEdicion);
- getBDeshacer().setEnabled(isEdicion);
- }
- /**
- * This method initializes jScrollPane
- *
- * @return javax.swing.JScrollPane
- */
- private JScrollPane getJScrollPane() {
- if (jScrollPane == null) {
- jScrollPane = new JScrollPane();
- jScrollPane.setBounds(new Rectangle(8, 27, 159, 179));
- jScrollPane.setViewportView(getJTable());
- }
- return jScrollPane;
- }
- /**
- * This method initializes jTable
- *
- * @return javax.swing.JTable
- */
- private JTable getJTable() {
- if (jTable == null) {
- jTable = new JTable();
- jTable.setAutoCreateColumnsFromModel(true);
- jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- jTable.setModel(model);
- Datos();
- jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
- @Override
- public void valueChanged(ListSelectionEvent e) {
- // TODO Auto-generated method stub
- ActualizarTextBoxes();
- }
- });
- }
- return jTable;
- }
- private void Datos() {
- String sql = "SELECT * FROM Ciudades";
- try {
- ControladorBD.addSql(sql);
- ControladorBD.execute();
- while(ControladorBD.finRecordSet()){
- String[] registro = {
- ControladorBD.getInt("ciu_codigo").toString(),
- ControladorBD.getString("ciu_descripcion") };
- model.ciudades.add(registro);
- }
- }
- catch (Exception e){
- }
- }
- private void actualizarJTable(){
- String codigoActual = getJCodigo().getText();
- Integer resultado = -1;
- for (int i=0; i< model.ciudades.size(); i++) {
- if(model.ciudades.get(i)[0].equals(codigoActual)){
- resultado = i;
- }
- }
- getJTable().getSelectionModel().setSelectionInterval(resultado, resultado);
- }
- public void ActualizarTextBoxes(){
- int indice = jTable.getSelectedRow();
- System.out.println(indice);
- Integer codigo = Integer.parseInt(model.ciudades.get(indice)[0]);
- String sql = "SELECT * FROM Ciudades WHERE ciu_codigo =" + codigo;
- try {
- ControladorBD.addSql(sql);
- ControladorBD.execute();
- if(ControladorBD.finRecordSet()){
- getJCodigo().setText(ControladorBD.getInt("ciu_codigo").toString() );
- getJDescripcion().setText(ControladorBD.getString("ciu_descripcion") );
- }
- } catch (Exception e) {
- // TODO: handle exception
- JOptionPane.showMessageDialog(null, "Ocurrio un error en la BD" + e.getMessage());
- e.printStackTrace();
- }
- }
- public void LimpiarTabla() {
- while(jTable.getRowCount()>0){
- ((DefaultTableModel)jTable.getModel()).removeRow(0);
- }
- }
- } // @jve:decl-index=0:visual-constraint="10,10"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement