Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Setting all items on a JComboBox

843806Sep 18 2007 — edited Sep 19 2007
On a JList, I can use setListData(Object[] listData) , to set a new set of objects on the list.

On a JComboBox, I can only add them one at a time using addItem(Object anObject).

This is fine, but ends up being much less efficient.

SO, I tried to get the model, and set items using the model. But with JComboBoxModel, you can only enquire and set the selection.

SO, to set all the items on a combo at once, I resulted to

JComboBox b = new JComboBox(items);
+((JComboBox)jButton).setModel(b.getModel());+

where jButton is an existing comboBox.

This seems to work, and be more efficient than adding items one at a time!

(You may ask, why not just set the items when I create the list. The reason is that this is a list
of fonts, and that takes a small while to generate. So I prefer to create the combo box on the UI,
and then create the font list on a slow thread, adding them to the comboBox when the list is
complete.)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 17 2007
Added on Sep 18 2007
3 comments
482 views