简体   繁体   中英

Selecting row and column of the Jtable by clicking the jCombobox that is inside a column of jtable

In my Jtable instance a column have the jComboBox , now what i want is to select the row and column of the cell once the value of selected jcombobox is changed.

If i use the actionPerformed event of the jcombobox, and get the jtable.getSelectedrow and column. System give me last selected row and column instead of current row and column.

Please guide me what to do .. thank you

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


            DefaultTableModel model = (DefaultTableModel) jTable.getModel();

            int selectedRow = jTable.getSelectedRow();
            int selectedColumn = jTable.getSelectedColumn();

            System.out.println("Row : " + selectedRow);
            System.out.println("Column : " + selectedColumn);
        } 

now what i want is to select the row and column of the cell once the value of selected jcombobox is changed.

Don't add an ActionListener to the combo box, that is not the way an editor of the table was designed to be used.

Instead you should be adding a TableModelListener to the TableModel of the JTable . An event will be generated whenever data is changed in the table. The event will contain the row/column of the cell that was changed.

For a working example check out: TableModelListener and multiple column validation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM