简体   繁体   中英

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.

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

I'd suggest you to implement your own ComboBoxModel:

public class YourComboBoxModel implements ComboBoxModel{

    @Override
    public Object getSelectedItem() {
        //return selected item Object;
    }

    @Override
    public void setSelectedItem(Object anItem) {
        //set selected item
    }

    @Override
    public Object getElementAt(int index) {
        //return the element based on the index
    }

    @Override
    public int getSize() {
        //return the size of your combo box list
    }

}

And build your JComboBox passing that model as parameter:

ComboBoxModel yourModel = new YourComboBoxModel();
JComboBox yourComboBox = new JComboBox(yourModel); 

Using a custom ComboBoxModel is the most flexible solution. It allows you to change the datastructure holding your datas, and the way you access it, modifying only the model you implemented instead of other unrelated part of code.

Whenever you need to work with editable models for these kinds of GUI elements it is always good to use a model. For the JComboBox you have an easy-to-use DefaultComboBoxModel .

It works easily:

DefaultComboBoxModel model = new DefaultComboBoxModel(new String[]{"Item1","Item2","Item3"});
JComboBox comboBox = new JComboBox(model);

in this way you have the model attached to the combobox, and it will display items from the array. Whenever you need to change them just do:

model.removeAllElements(); // if you need to empty it
model.addElement("New Item1");
model.addElement("New Item2");
model.addElement("New Item3");
model.fireContentsChanged();

and you'll have new items updated inside the GUI.

A bonus note: if you need to manage custom objects instead that strings you can easily add them to the JComboBox (in the sameway showed before), you just need to provide a custom public String toString() method that will manage the string representation.

In your example I don't get why you readd all the items everytime, you could just call addItem with the new String without removing everything and adding them back.

the best way to add some thing in your combo box in order that you can change it easily is to describe an array list first,if you would like to be dynamic you can user a text field then in a text field you can run an array list.

textField = new JTextField();
    textField.setBounds(131, 52, 86, 20);
    contentPane.add(textField);
    textField.setColumns(10);

then you have to create an array list

ArrayList al=new ArrayList();

then you have to equal your text field text to a string

String str=textfield.getText();

then add it to your array

al.add(str);

then add al item to your combo box.

JComboBox comboBox = new JComboBox();
    comboBox.setBounds(112, 115, 145, 20);
    contentPane.add(comboBox);
            comboBox.addItem(al);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM