Adding items to a JComboBox
Asked Answered
C

6

23

I use a combo box on panel and as I know we can add items with the text only

    comboBox.addItem('item text');

But some times I need to use some value of the item and item text like in html select:

    <select><option value="item_value">Item Text</option></select>

Is there any way to set both value and title in combo box item?

For now I use a hash to solve this issue.

Contrapuntal answered 26/7, 2013 at 17:56 Comment(0)
Z
52

Wrap the values in a class and override the toString() method.

class ComboItem
{
    private String key;
    private String value;

    public ComboItem(String key, String value)
    {
        this.key = key;
        this.value = value;
    }

    @Override
    public String toString()
    {
        return key;
    }

    public String getKey()
    {
        return key;
    }

    public String getValue()
    {
        return value;
    }
}

Add the ComboItem to your comboBox.

comboBox.addItem(new ComboItem("Visible String 1", "Value 1"));
comboBox.addItem(new ComboItem("Visible String 2", "Value 2"));
comboBox.addItem(new ComboItem("Visible String 3", "Value 3"));

Whenever you get the selected item.

Object item = comboBox.getSelectedItem();
String value = ((ComboItem)item).getValue();
Zito answered 26/7, 2013 at 18:2 Comment(4)
How can I select an item from the key ?Psychoactive
I'm getting this error: "ComboItem cannot be converted to String". How can I convert this? My code is exactly like this example.Towrope
I'm also getting the error: "ComboItem cannot be converted to String". Using Java 8Warr
It might be worth to mention -as it seems that some people have faced the same issue- that you need to declare the JComboBox with the right Type Parameters. See: #58789983 and #58789983Warr
S
3

You can use any Object as an item. In that object you can have several fields you need. In your case the value field. You have to override the toString() method to represent the text. In your case "item text". See the example:

public class AnyObject {

    private String value;
    private String text;

    public AnyObject(String value, String text) {
        this.value = value;
        this.text = text;
    }

...

    @Override
    public String toString() {
        return text;
    }
}

comboBox.addItem(new AnyObject("item_value", "item text"));
Smashed answered 26/7, 2013 at 18:33 Comment(0)
S
2

addItem(Object) takes an object. The default JComboBox renderer calls toString() on that object and that's what it shows as the label.

So, don't pass in a String to addItem(). Pass in an object whose toString() method returns the label you want. The object can contain any number of other data fields also.

Try passing this into your combobox and see how it renders. getSelectedItem() will return the object, which you can cast back to Widget to get the value from.

public final class Widget {
    private final int value;
    private final String label;

    public Widget(int value, String label) {
        this.value = value;
        this.label = label;
    }

    public int getValue() {
        return this.value;
    }

    public String toString() {
        return this.label;
    }
}
Schwa answered 26/7, 2013 at 18:50 Comment(0)
C
2

create a new class called ComboKeyValue.java

    public class ComboKeyValue {
        private String key;
        private String value;
    
        public ComboKeyValue(String key, String value) {
            this.key = key;
            this.value = value;
        }
        
        @Override
        public String toString(){
            return key;
        }
    
        public String getKey() {
            return key;
        }
    
        public String getValue() {
            return value;
        }
}

when you want to add a new item, just write the code as below

 DefaultComboBoxModel model = new DefaultComboBoxModel();
    model.addElement(new ComboKeyValue("key", "value"));
    properties.setModel(model);
Crocus answered 12/10, 2020 at 2:23 Comment(0)
F
0

Method call setSelectedIndex("item_value"); doesn't work because setSelectedIndex use sequential index.

Forsta answered 8/7, 2015 at 21:0 Comment(1)
This has nothing to do with the question, or any answers, and should be a comment, not an answer, anyway.Naval
E
-1

You can use String arrays to add jComboBox items

String [] items = { "First item", "Second item", "Third item", "Fourth item" };

JComboBox comboOne = new JComboBox (items);
Emilyemina answered 9/1, 2020 at 7:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.