简体   繁体   English

通过单击jtable列内的jCombobox选择Jtable的行和列

[英]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. 在我的Jtable实例中,一列具有jComboBox,现在我想要的是一旦选定的jcombobox的值更改后,就选择单元格的行和列。

If i use the actionPerformed event of the jcombobox, and get the jtable.getSelectedrow and column. 如果我使用jcombobox的actionPerformed事件,并获取jtable.getSelectedrow和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. 现在,我想要的是一旦选定的jcombobox的值更改后,就选择单元格的行和列。

Don't add an ActionListener to the combo box, that is not the way an editor of the table was designed to be used. 不要将ActionListener添加到组合框中,这不是设计表编辑器的方式。

Instead you should be adding a TableModelListener to the TableModel of the JTable . 相反,您应该向JTableTableModel中添加TableModelListener 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 有关工作示例,请查看: TableModelListener和多列验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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