JComboBox<BeanObject> not displaying edited values

52 views Asked by At

I have a Jtable, whose first column uses a JComboBox as editor. The combobox model contains data objects fetched from a sql database. If I manually enter a value inside the combobox and then leave the editor, the entered value is lost. This doens't happen if the value is selected from the popup, or if the JComboBox's model is instantiated with simple Strings instead of bean objects. Note that I need to add row dinamically, but this seems irrelevant since the issue appears both in the default row and in the added rows.

This is a working NetBeans sample project that reproduces my issue: https://drive.google.com/open?id=0B89FsS48-Yy4V09YRVozRzJGMkk

Here is the relevant code:

public NewJFrame() {
    initComponents();
    //bean objects used to populate the combobox:
    Item item1 = new Item("one", 1);
    Item item2 = new Item("two", 2);
    Item item3 = new Item("three", 3);
    JComboBox<Item> comboBox = new JComboBox<>(new Item[]{item1, item2, item3});
    comboBox.setEditable(true);
    DefaultCellEditor defaultCellEditor = new DefaultCellEditor(comboBox);
    defaultCellEditor.setClickCountToStart(1);
    jTable1.getColumnModel().getColumn(0).setCellEditor(defaultCellEditor);
}

private void addRowButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
    ((DefaultTableModel) jTable1.getModel()).addRow(new Object[]{null, null});
} 

Update: when the JComboBox in the table is populated via a model instead of the constructor like this,

items = new Item[]{item1, item2, item3};
JComboBox<Item> comboBox = new JComboBox<>();
comboBox.setModel(new DefaultComboBoxModel<>(items));

a manually inserted value is kept displayed, only if no selection has been made before; that is, if I first select a choice and then edit the choice, when leaving the combobox the previously selected item reappears. None of the reported behaviours occur in a JComboBox outside the table, so this led me to think it's something related to the CellEditor.

Update 2: here's a bug report of this issue from the year 2000! They said they solved it back then but this is far from solved after 15 years.

0

There are 0 answers