views:

696

answers:

4

I have a Swings GUI which contains a JComboBox and I want to load data into it from the database.

I have retrieved the data from the database in a String Array. Now how can I populate this String array into the JComboBox

EDITED====================================================================

In actual, the JComboBox is already instantiated when the java GUI is shown to the user. So I can't pass the Array as a paramter to the constructor.

How can I populate the already instantiated JComboBox?

The following is the code that is Nebeans generated code.

jComboBox15 = new javax.swing.JComboBox();

jComboBox15.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "12" }));

jComboBox15.setName("jComboBox15");

Can I set another ComboBoxModel to the above jComboBox?

+1  A: 
new JComboBox(stringArray);

A useful tip - when you know what class you are working with, check its javadoc. It most often contains the information you need.

Edit: after your update, use:

for (String string : stringArray) {
   comboBox.addItem(string);
}

(my tip still applies)

Bozho
Could you please look at the edited question?
Yatendra Goel
+1  A: 

Here's an excellent article about it: How to use Combo Boxes ( The Java Tutorial )

Basically:

String[] dbData = dateFromDb();
JComboBox dbCombo = new JComboBox(dbData);

You'll need to know other things like

  • Using an Uneditable Combo Box
  • Handling Events on a Combo Box
  • Using an Editable Combo Box
  • Providing a Custom Renderer
  • The Combo Box API
  • Examples that Use Combo Boxes

That article contains information about it.

EDIT

Yeap, you can either do what you show in your edited post, or keep a reference to the combo model:

DefaultComboBoxModel dcm = new DefaultComboBoxModel();
combo.setModel( dcm );
....
for( String newRow : dataFetched ) {
    dcm.addElement( newRow )
}
OscarRyz
Could you please look at the edited question?
Yatendra Goel
+3  A: 

Ah, the combo box is already instantiated.... In that case just clear the contents and add the new array item by item:

comboBox.removeAllItems();

for(String str : strArray) {
   comboBox.addItem(str);
}

Make sure this is done from the EDT!

Jason Nichols
+1  A: 

I think that what NetBeans does is what you need.

From wherever you want, you can create a DefaultComboBoxModel object and then invoke comboBox.setModel(defaultComboBox);

Here is a very small example of what I think you want to do: when the user clicks the button "Change data" the comboBox is filled with data from an array (method actionPerformed).

public class TestJComboBox extends JFrame {
    private JComboBox comboBox = new JComboBox();

    public TestJComboBox() {

        JButton changeComboBoxData = new JButton("Change data");
        changeComboBoxData.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                DefaultComboBoxModel cbm = new DefaultComboBoxModel(
                        new String[] { "hola", "adios" });
                comboBox.setModel(cbm);
            }
        });

        super.setLayout(new BorderLayout(10, 10));
        super.setSize(100, 100);
        super.add(changeComboBoxData, BorderLayout.NORTH);
        super.add(comboBox, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        new TestJComboBox().setVisible(true);
    }
}
Rambaldi