Search code examples
javaswingcombobox

java swing combobox


I have a combo box and a string array that holds all the values of the combo box in it. I erase the items from the combo box and then want to add in the values from the string array. It doesn't seem to let me just add in a string array. And I tried to itterate through the string adding items one by one but won't let me do that (or atleast the way I wrote it, it won't work).

May seem like a stupid question but I am new to working with swing in java.

Here's the code where I want to "reload" the items from the combo box:

String str = JOptionPane.showInputDialog(null, "Enter Name: ", "", 1);
        if(str != null){
              JOptionPane.showMessageDialog(null, "New name added: " + str, "", 1);
              nameCreator.addName(strNames, str);
              strNames = NameLoader.getNames();
              nameList.removeAllItems();
              nameList.addItem(strNames);
        }

EDIT: Made small typo and didn't realize what was wrong. Working now. Thanks for everyones help.


Solution

  • Did you used the method addItem(Object anObject)?

    You should iterate your array an use that method:

    String[] data = {a;b;c;d;e}
    for(int i=0; i < data.length; i++){
      comboBox.addItem(data[i]);
    }
    

    Luca