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

366 Views Asked by At

Got a current project going on and I need to set all cells from a column in a JTable to JComboBoxes. Their items are the same for all rows and are extracted from a SQL Server table. When the program runs, it fills the whole JTable with the values from a SQL Server table. 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. 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.

Is there a way to doing this easily?

This is how I'm adding the items to all the 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

There are 1 best solutions below

6
On

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)); and after super.getTableCellEditorComponent use the value to be set in the editor