Re: Loading a JComboBox
On 11/21/2010 3:14 PM, bruce wrote:
I am using NetBeans for my development.
I want to load a combobox with two pieces of information, namely the
data from a SELECT of a primary key and a datafield. How can I do
this?
I thought about creating an Object containing the Key and the
datafield, but couldn't figure out how to display the "datafield" in
the dropdown list.
It's pretty straightforward. There are several ways to do
it; here's one:
Object[] items = new Object[] { theKey, theDataField };
JComboBox cbox = new JComboBox(items);
Then add the combo box to your JPanel or whatever, and eventually to
a JFrame, pack() and setVisible(true).
Or if the combo box already exists in the GUI and you want to
change the list of items it displays and selects from:
Object[] items = ...as above...;
JComboBox cbox = ...the existing combo box...;
cbox.setModel(new DefaultComboBoxModel(items));
There are additional variations, too, but these should get you
started. If they don't, please explain your difficulty in more detail
than "couldn't figure out" and show us what you've tried.
--
Eric Sosman
esosman@ieee-dot-org.invalid