0

有没有办法仅使用 GUI 在 JCombobox Netbeans Swing Matisse 中设置键值字符串映射/对?下面的屏幕截图允许插入单个列表 JComboBox 列表数据

但是有没有办法使用 Matisse GUI 而不是像这样的代码插入 Map/Key-Value String Pair

Value - Display
_____   _____
ITEM1 - Item 1
ITEM2 - Item 2
ITEM3 - Item 3
ITEM4 - Item 4

如在 HTML 中选择选项标记存储值和显示值。

4

2 回答 2

2

这就是为什么我要询问使用 GUI 而不是代码

不要依赖 IDE 为您编写/生成代码。代码永远不会是可移植的。

也许您可以创建一个包含所有键/值对的文本文件。然后创建一个简单的例程,读取每个文件解析数据并将自定义对象添加到 ComboBoxModel。

有关此类自定义对象的示例,请查看带有隐藏数据的组合框。它是一个简单的对象,它覆盖 toString() 方法以在组合框中显示值。

对于那些建议您应该使用自定义渲染器的人,他们只对了一半。查看带有自定义渲染器的组合框,它允许您使用自定义渲染器而不会破坏组合框的默认功能。

于 2014-02-14T16:01:40.190 回答
1

“只使用图形用户界面?”

我假设您的意思是从设计角度来看。我不这么认为。只需手动编码即可。这并不难。

这是一个使用Student对象作为映射值的示例,并将Student id用作映射key。是 中的key显示值JComboBox。使用地图从选择中检索该值get(id)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class MapCombo {

    public MapCombo() {
        Map<Integer, Student> map = createMap();
        JComboBox cbox = createComboBox(map);
        cbox.setBorder(new EmptyBorder(20, 20, 20, 20));

        JFrame frame = new JFrame("Map ComboBox");
        frame.add(cbox);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private Map<Integer, Student> createMap() {
        Map<Integer, Student> map = new HashMap<>();
        Student s1 = new Student(23, "Micheal Jordan");
        Student s2 = new Student(6, "Lebron James");
        Student s3 = new Student(3, "Chris Paul");
        Student s4 = new Student(8, "Kobe Briant");
        Student s5 = new Student(21, "Tim Duncan");

        map.put(s1.getId(), s1);
        map.put(s2.getId(), s2);
        map.put(s3.getId(), s3);
        map.put(s4.getId(), s4);
        map.put(s5.getId(), s5);

        return map;
    }

    private JComboBox createComboBox(final Map<Integer, Student> map) {
        final JComboBox cbox = new JComboBox();
        for (Integer id : map.keySet()) {
            cbox.addItem(id);
        }

        cbox.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Integer id = (Integer)cbox.getSelectedItem();
                System.out.println(map.get(id));
            }
        });

        return cbox;
    }

    public class Student {

        String name;
        Integer id;

        public Student(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public Integer getId() {
            return id;
        }

        @Override
        public String toString() {
            return "Name: " + name + " - Stud ID: " + id;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MapCombo();
            }
        });
    }
}
于 2014-02-14T07:45:57.250 回答