2

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:

  1. the new row of the jtable is added correctly.
  2. the getElementAt method is called several times when clicked on the JComboBox and return all the available values (including the new ones).
  3. the method getSize() is called when I click the JComboBox and return the right updated value
  4. If JComboBox has focus I can use arrows to select the available items and all items are selectable (even the added ones).
  5. 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..

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
  • Maybe call combo.revalidate() after adding an item. – toto2 Aug 16 '11 at 19:19
  • @toto: I tried but revalidate not solve the problem – Heisenbug Aug 16 '11 at 19:23
  • Tried on a different platform? Or different look-and-fell? – toto2 Aug 16 '11 at 19:25
  • @toto: even changing look and feel nothing changes – Heisenbug Aug 16 '11 at 20:09
  • this idea works for me http://stackoverflow.com/questions/6261017/how-to-add-different-jcombobox-items-in-a-column-of-a-jtable-in-swing/6261853#6261853, but you are coding too hard things, maybe there are conflict betweens Listeners that you are forgot to remove from JComboBox if TableCell lost Focus, :-) no way without sscce :-) – mKorbel Aug 16 '11 at 21:08
  • @mKorbel: first of thanks for the attention. I know that my question is a bit vague and it's hard to get an answer. But I'm not using JComboBox inside the jtable like in the linked example. The JComboBox are in my program's gui inside a JPanel. Each combo box should show all the value of a particular column of an existing JTable the is synchronized with an SQL table of my db, but it's not inside the jtable itself. – Heisenbug Aug 16 '11 at 21:13
  • @0verbose that doesn't make me sence, JTable or JComboBox(es) contents is synchronized with DB, and isn't there some Beans, isn't it :-) – mKorbel Aug 16 '11 at 21:16
  • @mKorbel. No..no Beans. I retrieve some data from SQLITE database and display the data through JTables. – Heisenbug Aug 16 '11 at 21:18
  • and ComboModel == particular TableColumn ???, how did you linked/get data from JTable mabye from tableModel to ComboModel, and then are you using JComboBox for RowFiltering ??? – mKorbel Aug 16 '11 at 21:22
  • @mKorbel let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2552/discussion-between-0verbose-and-mkorbel) – Heisenbug Aug 16 '11 at 21:26

1 Answers1

2

I know that my question is a bit vague

Which is why a SSCCE is required.

Each combo box should show all the value of a particular column of an existing JTable

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • right suggestion by using your workaround for CellListener + 1 – mKorbel Aug 17 '11 at 07:20
  • @camickr: +1 . I think you are right. Even if should be possible write a custom model, in this situation maybe a TableModelListener is better. I didn't think about that. – Heisenbug Aug 17 '11 at 08:41