Adding items to a JComboBox

后端 未结 6 630
生来不讨喜
生来不讨喜 2020-12-01 18:16

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

相关标签:
6条回答
  • 2020-12-01 18:40

    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"));
    
    0 讨论(0)
  • 2020-12-01 18:49

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

    0 讨论(0)
  • 2020-12-01 18: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();
    
    0 讨论(0)
  • 2020-12-01 18:53

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 18:55

    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);
    
    0 讨论(0)
  • 2020-12-01 18:57

    You can use String arrays to add jComboBox items

    String [] items = { "First item", "Second item", "Third item", "Fourth item" };
    
    JComboBox comboOne = new JComboBox (items);
    
    0 讨论(0)
提交回复
热议问题