What the getValueAt (i, 0) method returns; if you can not find more cells in a jTable

1

I am comparing the contents of the cells of a jTable with a String specific, if it finds it proceeds to the corresponding action, but if it does not find it, what does it return?

while(!(TabVentas.getValueAt(i, 0).toString().equals(fila[0]))) {
    i++;
}

The while will be repeated until the two String match: getValueAt(i,0).toString and fila[0] .

    
asked by Jorge Gonzalez 07.03.2016 в 20:53
source

1 answer

2

As I see the source code of javax.swing.JTable , this is found:

public Object getValueAt(int row, int column) {
    return getModel().getValueAt(convertRowIndexToModel(row),
                                 convertColumnIndexToModel(column));
}

The result of JTable#getModel() returns an element that belongs to the TableModel interface. This model can be indicated in the constructor of JTable , since none is indicated, a default value will be used. I put the relevant code:

public JTable() {
    this(null, null, null);
}

public JTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm) {
    //...
    if (dm == null) {
        dm = createDefaultDataModel();
    }
    setModel(dm);
}

//...

protected TableModel createDefaultDataModel() {
    return new DefaultTableModel();
}

Therefore, the result of JTable#getValueAt , assuming that you do not manually assign any TableModel , will depend on the implementation of DefaultTableModel#getValueAt , whose code is:

public Object getValueAt(int row, int column) {
    Vector rowVector = (Vector)dataVector.elementAt(row);
    return rowVector.elementAt(column);
}

Seeing that this model is based on Vector is, the result of looking for an element whose indexes are not in the table, ArrayIndexOutOfBoundsException will be obtained.

You can check the sources online at the following URLs:

If you want to browse all available rows in your JTable , it would be best to use TableModel#getRowCount :

TableModel tm = TabVentas.getModel();
for (int i = 0; i < tm.getRowCount(); i++) {
    //hacer algo con este valor...
    TabVentas.getValueAt(i, 0);
}
    
answered by 07.03.2016 / 21:07
source