JComboBox with dynamic list?

R

Ramon F Herrera

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
 
O

Oliver Hirschi

Ramon F Herrera said:
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).

java.util.Vector
 
T

Thomas Hawtin

Ramon said:
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/...Box.html#JComboBox(javax.swing.ComboBoxModel)

You can write a List which fires events when modified, but that is an
unusual practice.

Tom Hawtin
 
R

Roedy Green

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()]);
 
T

Thomas Hawtin

Oliver said:
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.)
 
Z

zero

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

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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top