I am having a display issue when changing list elements within a model that is attached to a JComboBox. Using the attached code, perform the following:
click on button "list2"
select any element in the Combobox.
click on button "list1".
try to select an element in the combobox.
the popup display is of the correct length, but the list is empty.
THis problem occurs when selecting an element from a smaller list as the FIRST ACTION. If I select from a longer list first (click on list one and select anythingthen go to list 2) this behavior does not appear.
I am on a Macintosh running Snow Leopard (10.6) and using Java 1.6.0
I have attached a screen shot showing the issue as well as placing code in the body of the text.
You're ComboBoxModel implementation needs to fire the appropriate events to any ListDataListener that's added to it. The JComboBox adds at least one such listener itself. So:
- keep a List of ListDataListener objects that you modify from the addListDataListener and removeListDataListener methods.
- in the setList method, you first call intervalRemoved with 0 and the old size - 1 as the bounds (skipping if the old size is 0), then intervalAdded with 0 and the new size - 1 as the bounds (skipping if the new size is 0).
ok that worked! The code I had sent was a simplified example of the behavior I was using in a more complex application, and I had not included the implementation of the add and remove listener methods. What I had not done was do the ListDataEvent explicit updating. Instead of using the intervalDeleted and intervalAdded I used a single CONTENTS_CHANGED event and that seemed to work fine. Do you see any issues with that? Basically I change the editList to this: