开发者

how to fill JTextFields with the columns of a JTable search?

开发者 https://www.devze.com 2023-01-16 08:36 出处:网络
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 th

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 se开发者_如何学Pythonarch 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


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();
            }
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消