Error filling JTextField with JTable data [closed]

1

I have the problem that my program returns an error, that I do not know why it occurs, when collecting data from a row of a JTable selected and pass them to a series of JTextField , the error produced is this:

  

java.lang.ArrayIndexOutOfBoundsException: 0 > = 0

I attach code where I load the data in the textField

private void btModificarActionPerformed(java.awt.event.ActionEvent evt) {                                            

        int rowIndex = tabla.getSelectedRow();
        dni = String.valueOf(tabla.getValueAt(rowIndex,0));
        nombre = String.valueOf(tabla.getValueAt(rowIndex,1));
        ciudad = String.valueOf(tabla.getValueAt(rowIndex,2));


        tfDni.setText(dni);
        tfNombre.setText(nombre);
        tfCiudad.setText(ciudad);

        btInsertar.setEnabled(false);
        btBorrar.setEnabled(false);
        btListado.setEnabled(false);

    } 

Here I call the modification:

 private void btAceptarActionPerformed(java.awt.event.ActionEvent evt) {                                          
        modificar();  
    } 

Here already if I make the modification:

public void modificar(){
       int rowIndex = tabla.getSelectedRow();

       try{      
       String dni = String.valueOf(tabla.getValueAt(rowIndex, 0));
       String nombre = String.valueOf(tabla.getValueAt(rowIndex, 1));
       String ciudad = String.valueOf(tabla.getValueAt(rowIndex, 2));
       String sql = "update clientes set nombre='"+nombre+"',ciudad='"+ciudad+"' where dni='"+dni+"'";
       /*PARA HACER QUE EL UPDATE COJA LOS VALORES DEL CAMPO DE TEXTO, HABRÍA QUE PASARLE EL tf.getText() que corresponda*/
       mysql.ejecutar(sql);
       }catch(Exception e){
           JOptionPane.showMessageDialog(null, "Error al Modificar");
       }
    }

The application's constructor is like that, I think the fault may be there.

public class Ventana extends javax.swing.JFrame {
    MySQL mysql;
    Connection con;
    PreparedStatement pst;
    Statement st;
    DefaultTableModel modelo;
    String dni;
    String nombre;
    String ciudad;
    ResultSet rs;

public Ventana(MySQL mysql) throws SQLException {
    /*INSTANCIAMOS MYSQL*/
    this.mysql = new MySQL();
    initComponents();
    this.setVisible(true);
    con = mysql.MySQLConnection("pedidos", "root", "1234");
    modelo = new DefaultTableModel();
    tabla.setModel(modelo);

}

Here is the photo so you can see everything

Here is another photo that shows the whole code

Finally, I have attached the method where I show what is in the database and I extract the metadata from the columns

when choosing the row, it returns that

returns -1, that means, in my opinion, that has not updated, and indeed it is.

Sorry for the delay in responding

    
asked by scorpions 05.06.2017 в 10:37
source

2 answers

1

The detail would be that you want to access the data without having selected any specific row of the jTable, to avoid this error it is enough to add an if conditioner before the execution:

// Sample code

private void btModificarActionPerformed(java.awt.event.ActionEvent evt) { 

if (tabla.getSelectedRow() != -1) {
    int rowIndex = tabla.getSelectedRow();
    dni = String.valueOf(tabla.getValueAt(rowIndex,0));
    nombre = String.valueOf(tabla.getValueAt(rowIndex,1));
    ciudad = String.valueOf(tabla.getValueAt(rowIndex,2));


    tfDni.setText(dni);
    tfNombre.setText(nombre);
    tfCiudad.setText(ciudad);

    btInsertar.setEnabled(false);
    btBorrar.setEnabled(false);
    btListado.setEnabled(false);
}

}

Obs: An addition to the code would be to notify the user that he can not modify the data if he has no record selected, or to keep the button blocked until at least one record is selected.

    
answered by 05.06.2017 / 16:22
source
1

It is by the column number of your ID, you recovered at the beginning using the column zero (0), but when you want to save in the database you are using the column one (1)

    
answered by 07.06.2017 в 19:26