【问题标题】:2D JComboBox that one controling items of the other with ActionListener一个用 ActionListener 控制另一个项目的 2D JComboBox
【发布时间】:2016-12-04 03:36:50
【问题描述】:

我坚持在 Java GUI 表单中制作两个下拉菜单,第一个的选择将决定第二个菜单中的选择。

我希望达到的效果如下所示: enter image description here

当我在 comboBox1 中切换选择后,它看起来像这样: enter image description here

这是我的测试代码:

    public static void main(String[] args) {
        Tester tester = new Tester();
        String[] flower = {"Rose", "Tulip"};
        String[] color1 = {"Yellow", "Blue", "Red"};
        String[] color2 = {"Purple", "White", "Green"};

        for (String flowerPicked : flower) {
            tester.comboBox1.addItem(flowerPicked);
        }
        tester.comboBox1.addActionListener(e -> {
            // remove previous items in comboBox2 everytime a new item in box1 is selcted
            tester.comboBox2.removeAllItems();
            String flowerChoice = tester.comboBox1.getSelectedItem().toString();
            if (flowerChoice.equalsIgnoreCase("Rose"))
                for (String colorPicked : color1) {
                    tester.comboBox2.addItem(colorPicked );
                }
            else
                for (String type : color2) {
                    tester.comboBox2.addItem(type);
                }
        });    
        tester.comboBox2.addActionListener(e -> {
            String colorChoice = tester.comboBox2.getSelectedItem().toString();
            String flowerChoice = tester.comboBox1.getSelectedItem().toString();
            system.out.println(colorChoice + " " + flowerChoice);
        });
    }

但是每次我尝试在comboBox1中切换我的选择时,我总是在removeAllItems()和comboBox2.getSelectedItems()处遇到NullPointerException。

我尝试调试它,但似乎是因为每当程序执行 removeAllItems() 和 comboBox2.addItem() 时都会调用 comboBox2 的 actionListener。我不知道如何处理这个

有点帮助?​​

【问题讨论】:

    标签: java swing user-interface actionlistener jcombobox


    【解决方案1】:

    您是对的,从 JComboBox 中删除所有项目会导致其 ActionListener 触发并返回 null 选择。

    可能的解决方案:

    1. 在删除所有项目之前从 JComboBox 中删除所有 ActionListener,然后在完成后替换侦听器。 -- 或者 --
    2. 不要在返回的项目上调用toString()(这就是引发NPE 的原因——在空引用上调用toString()),而是将返回的选定项目转换为字符串。演员表不会抛出 NPE。

    第一个例子:

    ActionListener[] actionListeners = tester.comboBox2.getActionListeners();
    for (ActionListener actionListener : actionListeners) {
        tester.comboBox2.removeActionListener(actionListener);
    }
    tester.comboBox2.removeAllItems();
    String flowerChoice = tester.comboBox1.getSelectedItem().toString();
    if (flowerChoice.equalsIgnoreCase("Rose"))
        for (String colorPicked : color1) {
            tester.comboBox2.addItem(colorPicked);
        }
    else {
        for (String type : color2) {
            tester.comboBox2.addItem(type);
        }
    }
    for (ActionListener actionListener : actionListeners) {
        tester.comboBox2.addActionListener(actionListener);
    }
    

    第二个例子:

    String colorChoice = (String) tester.comboBox2.getSelectedItem();
    String flowerChoice = (String) tester.comboBox1.getSelectedItem();
    System.out.println(colorChoice + " " + flowerChoice);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      • 2020-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多