开发者

Suggestion on how to debug ComboBoxModel

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 getEl开发者_运维知识库ementAt(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..


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜