【问题标题】:Netbeans Swing Matisse JCombobox Key-Value PairNetbeans Swing Matisse JCombobox 键值对
【发布时间】:2014-03-13 08:16:49
【问题描述】:

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

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

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

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

【问题讨论】:

  • 你能否进一步解释你想要达到的目标,答案可以是肯定的,也可以是否定的
  • @peeskillet 请查看,问题已更新。
  • 那么显示是关键?

标签: java swing netbeans jcombobox matisse


【解决方案1】:

" 仅使用 GUI 吗?"

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

这是一个使用 Student 对象作为映射值,Student id 作为映射 key 的示例。 keyJComboBox 中的显示值。使用地图中的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();
            }
        });
    }
}

【讨论】:

  • 其实我有很多 JCombobox (Key-Value) 的形式。这就是为什么我要询问使用 GUI 而不是代码。
【解决方案2】:

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

不要依赖 IDE 为您编写/生成代码。代码永远无法移植。

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

有关此类自定义对象的示例,请查看Combo Box With Hidden Data。它是一个简单的对象,它覆盖 toString() 方法以在组合框中显示值。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    相关资源
    最近更新 更多