• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using a static list for multiple JComboBoxes  RSS feed

 
John Duke
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My application has several instances of a Location (extended JComboBox), which uses a DefaultComboBoxModel (mutable). When I create the DefaultComboBoxModel, I give it a static Vector of Strings. The list is static because there may be several hundred entries and I don't really want multiple copies. When a new entry is entered in one of the comboboxes, I'd like to have all the others be notified of the update, and have them 'refresh' the model somehow. I've tried ActionListeners and ListDataListeners, and I cannot seem to get the 'other' Location comboboxes to be notified. I would have thought the ListDataListener would be triggered, but it doesn't seem to be.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ListDataListener will notify you of changes to the backing data model.
ActionListener or ItemListener are the way to go. Recommended reading http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#listeners
If you still cannot get it to work, post your SSCCE code here so we can take a look.

 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Duke wrote:When I create the DefaultComboBoxModel, I give it a static Vector of Strings.

Don't share the underlying data, share the model. By using the same model for all your combos, they will all be notified of any change.

And you won't require a static member for storing the data.
 
John Duke
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was it. I needed to 'share' the model, not just the list data. Somehow, I got it into my head that the models were the view of the data, so I thought I needed each combobox to have its own model. After makng the model static, I got the notifications I needed. Thanks, Darryl! And I didn't need the ListDataListener; the ActionListener got me what I needed, too. Thanks, Maneesh!

Oops. I spoke too soon. Sharing the model did what I thought it would: the same selection appears in all of the comboboxes. Not what I wanted. I will investigate further and post what I find out.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an idea running around in my head, but I have to go out now. If nothing else surfaces in the meantime, I may do some experiments and post back in a few hours.

In a very general way, my idea is to write a ComboBoxModel that wraps a DefaultListModel and forwards adding/removing ListDataListeners and other ListModel methods. The each of your combos can have its own model and its own selection while sharing the data in the list model. I don't know whether that will work until I test it.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is more or less what I had in mind. I usually miss something so this may be considered as a proof of concept rather than a fully working code. Do not use this witout adequate testing.
edit Like I said, I usually miss something :/
 
John Duke
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I finally bit the bullet and created a standalone version of the problem I'm seeing. Here's three editable combo boxes that share the data in a Vector<String>. When I insert a new entry in the model in one of the boxes, the dropdown lists in the other two boxes vanish. Interestingly enough, if all three boxes' initial value is the first in the list, the entries do not vanish in the other two when one has an entry added. (Let me apologize in advance for the different code formatting than what I've typically seen here. I like it this way, though.)

 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Duke wrote:I needed to 'share' the model, not just the list data. Somehow, I got it into my head that the models were the view of the data, so I thought I needed each combobox to have its own model. After makng the model static, I got the notifications I needed.

Oops. I spoke too soon. Sharing the model did what I thought it would: the same selection appears in all of the comboboxes.


You can't quite share the model, though that's the right way to think about the problem. What you want to do is share the getElementAt()/getSize() part of the model but not the getSelectedItem()/setSelectedItem() part.

Darryl Burke has the right approach, but his code needs to also override getSelectedItem()/setSelectedItem() or else they will attempt to refer DefaultComboBoxModel's internal Vector. I think it would be best to replace "extends DefaultComboBoxModel" with "implements ComboBoxModel". ["implements MutableComboBoxModel" might be even better, but then would also have to write implementations for addElement()/insertElementAt()/removeElement()/removeElementAt(), which would not be difficult.]
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Cole wrote:Darryl Burke has the right approach, but his code needs to also override getSelectedItem()/setSelectedItem() or else they will attempt to refer DefaultComboBoxModel's internal Vector.

Um, no, they don't. DefaultComboBoxModel has a field selectedItem which is independent of the data vector.
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:
Um, no, they don't. DefaultComboBoxModel has a field selectedItem which is independent of the data vector.


Yes you are correct. I apologize. It's addElement()/insertElementAt()/removeElement()/removeElementAt() that access the Vector.

I would still recommend not subclassing DefaultComboBoxModel and inheriting the unused Vector at all.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!