I am looking for a way to associate a JComboBox to a *dynamic*
(growing) list of contents.
What I have so far is this:
(1) Read the items one at a time and add them to an ArrayList like
this:
placeList.add(item);
(2) After the above loop is done, convert the ArrayList to a fixed
length array:
int howMany = placeList.size();
String[] placeArray = new String[howMany];
placeList.toArray(placeArray);
(3) Finally, I can create a new ComboBox, which seems to prefer
fixed-length lists:
dropDownList = new DefaultComboBoxModel(placeArray));
Due to some other considerations, the above method has become very
inconvenient, and I would definitely prefer to bind the JComboBox to an
ArrayList (or anything else that is allowed to grow).
Thanks for your kind assistance.
-Ramon F Herrera
Oliver Hirschi - 27 Dec 2005 09:11 GMT
> I am looking for a way to associate a JComboBox to a *dynamic*
> (growing) list of contents.
[quoted text clipped - 21 lines]
> inconvenient, and I would definitely prefer to bind the JComboBox to an
> ArrayList (or anything else that is allowed to grow).
java.util.Vector

Signature
Oliver Hirschi
http://www.FamilyHirschi.ch
Thomas Hawtin - 27 Dec 2005 09:56 GMT
>>(3) Finally, I can create a new ComboBox, which seems to prefer
>>fixed-length lists:
[quoted text clipped - 9 lines]
>
> java.util.Vector
DefaultComboBoxModel(Vector<?>) just copies the reference. If you alter
the Vector the model changes but does not fire an event. Disaster! Not a
particularly clever part of Swing.
Whereas JComboBox(Vector<?>) uses the "default" model, JList(Vector<?>)
does its own thing. It creates a fixed length ListModel that again fails
to copy the data. If you change the Vector the model change but does not
fire an event, and if you shorten the Vector then the JList will become
a tad unwell. Not a particularly clever part of Swing.
Stick to the models.
Tom Hawtin
(In my other post I read (3) as using the JComboBoxModel constructors,
but the comments still stand.)

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Thomas Hawtin - 27 Dec 2005 09:19 GMT
> Due to some other considerations, the above method has become very
> inconvenient, and I would definitely prefer to bind the JComboBox to an
> ArrayList (or anything else that is allowed to grow).
If you make changes to a plain List, then there is no way for other
objects to notice the changes.
The standard way to have a component track data in Swing is to use a
model. Models fire events when they change, which components (and UI
delegates) listen to, modifying and repainting themselves as necessary.
For JComboBox the data model is ComboBoxModel, usually implements by
DefaultComboBoxModel.
http://download.java.net/jdk6/docs/api/javax/swing/JComboBox.html#JComboBox(java
x.swing.ComboBoxModel)
You can write a List which fires events when modified, but that is an
unusual practice.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Roedy Green - 27 Dec 2005 10:03 GMT
> int howMany = placeList.size();
> String[] placeArray = new String[howMany];
> placeList.toArray(placeArray);
you can collapse that down to a more conventional:
String[] placeArray = placeList.toArray(new String[placeList.size()]);

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
zero - 27 Dec 2005 18:57 GMT
> I am looking for a way to associate a JComboBox to a *dynamic*
> (growing) list of contents.
[quoted text clipped - 25 lines]
>
> -Ramon F Herrera
Forget DefaultComboBoxModel. Create your own ComboBoxModel which stores
the data in an ArrayList, and fires an event when the data changes. You
can base it on AbstractListModel which already has methods that fire
change events (add, change, remove). You just call the appropriate
method when changing the data. For convenience, you can also implement
an addAll(Collection c) method.
Then your code can be changed to:
MyComboBoxModel model = new MyComboBoxModel();
model.addAll(placeList);
JComboBox ddl = new JComboBox(model);
When adding an item, all you need is:
((MyComboBoxModel)ddl.getModel()).addItem(item);

Signature
Beware the False Authority Syndrome