in my project I've several JComboBox with their custom models. Basically they are used to show values of some JTables column (therefore I decided to implement them on the relative class extending AbstractTableModel).
public class MyTableModel1 extends AbstractTableModel{
protected class MyTableComboBoxModel1 extends AbstractListModel implements ComboBoxModel{
private Object selected;
@Override
public Object getElementAt(int index) {
return getValueAt(index, 1);
}
@Override
public int getSize() {
return getRowCount();
}
@Override
public Object getSelectedItem() {
return this.selected;
}
@Override
public void setSelectedItem(Object anItem) {
this.selected = anItem;
}
}
}
And I have several models : MyTableModel2 with MyTableComboBoxModel2. These models all do pretty the same thing except some additional operations not related neither with the combobox nor with the table itself.
The purpose of all this stuff should be to update JComboBox's displayed values accordingly to modifications occured to the relative JTable.
All works fine for models I've implemented except in one case , and after several hours of debug I still can't solve it. The code of the bugged model is almost identical to the others. Probably it's a bug somewhere else in my code, but I can't figure out where.
The wrong case has the following behavior: when I initially created a table with some values these are correctly displayed even in the JComboBox, but when I add a new row the displayed values become all blank (the size of the displayed blank menu is right). I found out that:
- the new row of the jtable is added correctly.
- the getElementAt method is called several times when clicked on the JComboBox and return all the available values (including the new ones).
- the method getSize() is called when I click the JComboBox and return the right updated value
- If JComboBox has focus I can use arrows to select the available items and all items are selectable (even the added ones).
- If I use the mouse clicking one point at random on the displayed blank menu, all the last available entry could be chosen.
Has someone any idea? Could you suggest me how could I debug in such a situation? Unfortunately I can't post an SSCCE.. I hope someone could help anyway..
Which is why a SSCCE is required.
I don't understand why you need a custom model. I would guess you just need to use a TabelModelListener.
Whenever a value is added/removed you updated the combo box.