views:

28

answers:

1

I have a master/detail form with a JTable on top, and all the JTextFields corresponding below in the JPanel. I'm trying to make a search in the JTable, so that when the correct row gets picked, all the JTextFields can be filled with the column values. I don't know how can I call the rows programmatically to do so. How would it be done?

This is the code I'm using to do the search:

int rows = (masterTable.getModel()).getRowCount();
final int colCedula = 1; //columna de la CEDULA
final int colRuc = 11; //columna de RUC
String value = null ;
for(int i=0; i
value = (String) (masterTable.getModel()).getValueAt(i, colCedula);
if (value.equals(this.txt_BuscaCliente.getText())) {
    //CODE FOR FILLING JTEXTFIELDS
}

If the search finds the column value and stops the loop, could I just write in the //CODE section masterTable.getSelectedRow() and then fill all the JTextFields with its column values???

Also, how is it done to have the row selected highlighted, programatically? Let's say, after my search finds the column value, to have that row highlighted in the JTable

+1  A: 

I'd start with the example in the tutorial article How to Use Tables: User Selections in order to understand list selection events. Given a SINGLE_SELECTION model, you won't have to search; just fill in the text fields from the selected row. Alternatively, you can make the cells editable in your table model, and you won't have to copy them at all.

Addendum:

Also, how is it done to have the row selected highlighted, programatically?

Instead of searching, let your implementation of ListSelectionListener tell you what selection has been made by the user. In the example cited, modify the RowListener as shown below to iterate through the columns in the selected row.

private class RowListener implements ListSelectionListener {

    @Override
    public void valueChanged(ListSelectionEvent event) {
        if (!event.getValueIsAdjusting()) {
            for (int c : table.getSelectedRows()) {
                int row = table.convertRowIndexToModel(c);
                TableModel model = table.getModel();
                for (int col = 0; col < model.getRowCount(); col++) {
                    System.out.println(model.getValueAt(row, col));
                }
                System.out.println();
            }
        }
    }
}
trashgod