JTable单元在点击打开JComboBox时失去价值

时间:2015-06-19 19:35:44

标签: java swing jdbc jtable jcombobox

当前项目正在进行中,我需要将JTable中的所有单元格设置为JComboBoxes。它们的项对于所有行都是相同的,并从SQL Server表中提取。程序运行时,它会使用SQL Server表中的值填充整个JTable。此时一切正常,但当用户点击列以显示JComboBox并且项目显示时, JComboBox会覆盖该单元格上的值。我想要发生的是打开JComboBox的值和"建议"向用户提供该单元格中的最后一个值(消失的那个),下拉并选择所述值。

有没有办法轻松做到这一点?

这就是我将这些项目添加到所有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));

1 个答案:

答案 0 :(得分:1)

您可以覆盖方法

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

在通话col_cod_tipo_verba.setCellEditor(new DefaultCellEditor(box_tab_tipo_verba));after super.getTableCellEditorComponent中使用要在编辑器中设置的值