简体   繁体   English

单击以打开JComboBox时JTable单元格丢失值

[英]JTable cell losing value when it's clicked to open a JComboBox

Got a current project going on and I need to set all cells from a column in a JTable to JComboBoxes. 有一个当前项目正在进行,我需要将JTable的列中的所有单元格设置为JComboBoxes。 Their items are the same for all rows and are extracted from a SQL Server table. 它们的项目对于所有行都是相同的,并且是从SQL Server表中提取的。 When the program runs, it fills the whole JTable with the values from a SQL Server table. 程序运行时,它将用SQL Server表中的值填充整个JTable Everything is ok at this point but when a user clicks the column to show the JComboBox and the items show up, the value that was on that cell is overwritten by the JComboBox . 此时一切正常,但是当用户单击列以显示JComboBox并显示项目时, 该单元格上的值将被JComboBox覆盖。 What I wanted to happen is opening the JComboBox with the values and "suggesting" to the user the last value in that cell (the one that disappeared), dropping down and selecting the said value. 我想要发生的是打开带有值的JComboBox,并向用户“建议”该单元格中的最后一个值(消失的那个),然后下拉并选择所述值。

Is there a way to doing this easily? 有办法轻松做到这一点吗?

This is how I'm adding the items to all the JComboBoxes 这就是我将项目添加到所有JComboBoxes

TableColumn col_cod_tipo_verba = jtab_verba.getColumnModel().getColumn(3); JComboBox box_tab_tipo_verba = new JComboBox(); ResultSet rs = Glob.conecta_sql().prepareStatement("SELECT * FROM tab_tipo_verba").executeQuery(); while (rs.next()) { box_tab_tipo_verba.addItem(rs.getString(1)); } col_cod_tipo_verba.setCellEditor(new DefaultCellEditor(box_tab_tipo_verba));

You can override the method 您可以覆盖方法

Component getTableCellEditorComponent(JTable table, Object value,
                                      boolean isSelected,
                                      int row, int column)

In the call col_cod_tipo_verba.setCellEditor(new DefaultCellEditor(box_tab_tipo_verba)); 在调用中col_cod_tipo_verba.setCellEditor(new DefaultCellEditor(box_tab_tipo_verba)); and after super.getTableCellEditorComponent use the value to be set in the editor after super.getTableCellEditorComponent使用要在编辑器中设置的值

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

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