Problem when obtaining the value of the cells of a jtable

0

A problem arises when I get the value of the cells of a specific column and it is that I want to obtain the order of those data, use:

   TableRowSorter sorter = new TableRowSorter(ModeloClientes);
    tablaCliente.setRowSorter(sorter);

that is the order in which he shows them to me if I click on the column, previously with the sql I give order of asc to the amount. But I also need the order in which the table updates it. I used this code but it still grabs me in the same order of sql.

private void tablaClienteMouseClicked(java.awt.event.MouseEvent evt) {                                          
    DefaultTableModel ModeloClientes = (DefaultTableModel) tablaCliente.getModel();
    if (evt.getClickCount() == 1)
    {
        for (int i = 0; i < tablaCliente.getRowCount(); i++) 
        {
            Object f = tablaCliente.getModel().getValueAt(i, 1);
            String d = f.toString();
            int numero = Integer.parseInt(d);
            ORDENALEATORIO[j]=numero;
            j++;
        }
    }
}

    
asked by Efrainrodc 24.03.2017 в 04:04
source

2 answers

0

Change your code for this:

private void tablaClienteMouseClicked(java.awt.event.MouseEvent evt) {                                          
        DefaultTableModel ModeloClientes = (DefaultTableModel) tablaCliente.getModel();
        if (evt.getClickCount() == 1)
        {
            for (int i = 0; i < tablaCliente.getRowCount(); i++) 
            {
                Object f = tablaCliente.getValueAt(i, 1);
                String d = f.toString();
                int numero = Integer.parseInt(d);
                ORDENALEATORIO[i]=numero;
            }
        }
    }

One: The getModel() should be from your direct table not from the model.

Second: I do not understand why you use the variable j if you can use your i index of the for cycle directly

    
answered by 24.03.2017 / 17:56
source
0

It happens that if you access the model of your JTable: tablaCliente.getModel().getValueAt(i, 1); in this way you are accessing the initial loading of your JTable, not your ordered table.
Change that line to this: tablaCliente.getValueAt(i, 1);

    
answered by 24.03.2017 в 17:09