如何在Swing中的JCombobox中将标签设置为显示并将值设置为值?

How to set Label as Display and value as value in JCombobox in Swing?

我的代码动态创建 Jcombobox 并从查询结果中填充项目。 此查询 returns 一个包含标签和值的对象。 我想在调用 combo.getSelectedItem() 时显示标签和 returns 值。 我看到这个 Example 搜索的时候没看懂

JComboBox<String> jComboBox=new JComboBox<String>();

if(dataSourceAttributeObjs!=null && dataSourceAttributeObjs.size()>0)
{
    jComboBox.addItem("Select");
    for(DataSourceAttributeObj dataSourceAttributeObj:dataSourceAttributeObjs)
    {
        jComboBox.addItem(dataSourceAttributeObj.getLabel());
    }
}

您提到的示例 (Set Value and Label to JComboBox) 描述了为组合框定义自定义渲染器的可能性,在您的情况下这似乎是正确的方法。

一段代码中nachokk的答案:

import java.awt.*;
import java.util.*;
import javax.swing.*;

public class CustomComboBoxRenderer {
    public static void main(final String[] arguments) {
        new CustomComboBoxRenderer().launchGui();
    }

    private void launchGui() {
        final JFrame frame = new JFrame("Stack Overflow: custom combo box renderer");
        frame.setBounds(100, 100, 800, 600);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        final JComboBox<Concept> comboBox = getComboBox();
        final JLabel label = new JLabel("Please make a selection...");

        comboBox.addActionListener(actionEvent -> {
            final Object selectedItem = comboBox.getSelectedItem();
            if (selectedItem instanceof Concept)
                label.setText(((Concept) selectedItem).getValue());
        });

        final JPanel panel = new JPanel(new BorderLayout());
        panel.add(comboBox, BorderLayout.NORTH);
        panel.add(label, BorderLayout.CENTER);
        frame.getContentPane().add(panel);

        frame.setVisible(true);
    }

    private JComboBox<Concept> getComboBox() {
        final List<Concept> concepts = Arrays.asList(new Concept("label 1", "value 1"),
                                                     new Concept("label 2", "value 2"),
                                                     new Concept("label 3", "value 3"));

        final JComboBox<Concept> comboBox = new JComboBox<>(new Vector<>(concepts));

        comboBox.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(final JList<?> list,
                                                          final Object value,
                                                          final int index,
                                                          final boolean isSelected,
                                                          final boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected,
                                                   cellHasFocus);
                if (value instanceof Concept)
                    setText(((Concept) value).getLabel());

                return this;
            }
        });

        return comboBox;
    }
}

Concept class 可能如下所示:

public class Concept {
    private final String label;
    private final String value;

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

    public String getLabel() {
        return label;
    }

    public String getValue() {
        return value;
    }
}

这对你有帮助

public class Java 扩展 JFrame {

private JComboBox cb;
private JLabel l;

private static String[] fileName = { "Image1.jpg", "Image2.jpg" };
private Icon[] pics = { new ImageIcon(getClass().getResource(fileName[0])),
        new ImageIcon(getClass().getResource(fileName[1])) };

Java() {



    super("Title");
    setLayout(new FlowLayout());

    cb = new JComboBox(fileName);

    cb.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {

            if(e.getStateChange() == ItemEvent.SELECTED)
                l.setIcon(pics[cb.getSelectedIndex()]);

        }
    });

    add(cb);
    l = new JLabel(pics [0]);
    add(l);

}

}

如果您只想使用名称和值,则可以使用枚举。 例如

  enum label
  {
     label1(0),label2(1);

  int value;
  label(int value)
       {
       this.value=value;
       }

 }

并分配组合框,

   `JComboBox box=new JComboBox(label.values());`

如果你想要它的价值, 获取标签对象并从中获取值 samp = box.SelectedItem().value