Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向JComboBox添加项_Java_Swing_Jcombobox - Fatal编程技术网

Java 向JComboBox添加项

Java 向JComboBox添加项,java,swing,jcombobox,Java,Swing,Jcombobox,我在面板上使用一个组合框,我知道我们只能添加文本项 comboBox.addItem('item text'); 但有时我需要使用一些项目值和项目文本,如html选择: <select><option value="item_value">Item Text</option></select> 项目文本 有没有办法在组合框项目中同时设置值和标题 现在我使用一个散列来解决这个问题。将值包装在一个类中,并重写toString()方

我在面板上使用一个组合框,我知道我们只能添加文本项

    comboBox.addItem('item text');
但有时我需要使用一些项目值和项目文本,如html选择:

    <select><option value="item_value">Item Text</option></select>
项目文本
有没有办法在组合框项目中同时设置值和标题


现在我使用一个散列来解决这个问题。

将值包装在一个类中,并重写
toString()
方法

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;
    }
}
将ComboItem添加到您的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"));
无论何时获得所选项目

Object item = comboBox.getSelectedItem();
String value = ((ComboItem)item).getValue();

可以将任何对象用作项目。在该对象中,可以有多个需要的字段。在您的例子中,是value字段。必须重写toString()方法来表示文本。在您的例子中是“项目文本”。请参见示例:

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"));
addItem(Object)接受一个对象。默认的JComboBox呈现程序对该对象调用toString(),这就是它显示为标签的内容

因此,不要将字符串传递给addItem()。传入其toString()方法返回所需标签的对象。该对象还可以包含任意数量的其他数据字段

尝试将其传递到您的组合框中,并查看其呈现方式。getSelectedItem()将返回对象,您可以将该对象转换回小部件以从中获取值

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;
    }
}

方法调用
setSelectedIndex(“item_值”)不起作用,因为
setSelectedIndex
使用顺序索引。

您可以使用字符串数组添加jComboBox项

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

JComboBox comboOne = new JComboBox (items);

创建一个名为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;
        }
}
当您想添加一个新项目时,只需编写如下代码

 DefaultComboBoxModel model = new DefaultComboBoxModel();
    model.addElement(new ComboKeyValue("key", "value"));
    properties.setModel(model);

谢谢,这就是我需要的。我怎样才能从键中选择一个项目呢?我遇到了一个错误:“ComboItem不能转换为字符串”。我如何转换这个?我的代码与此示例完全相同。我还收到错误:“ComboItem无法转换为字符串”。使用Java 8时,值得一提的是,您需要使用正确的
类型参数
声明JComboBox,因为有些人似乎也遇到过同样的问题。请看:这与问题或任何答案无关,应该是评论,而不是答案。