Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JComboBox display issue when changing models

 
davidb smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

launch code.
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

Any ideas?

Thanks

I have attached a screen shot showing the issue as well as placing code in the body of the text.
//////

TestComboProblem.png
[Thumbnail for TestComboProblem.png]
Screen shot showing problem
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
davidb smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


So thanks again!
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't suggest contentsChanged because I didn't know what to use for the indexes, but apparently 0 and -1 work. And you're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic