Setting all items on a JComboBox
843806Sep 18 2007 — edited Sep 19 2007On 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.)