Java JComboBox选择和项,并根据选择显示一个新列表

Java JComboBox选择和项,并根据选择显示一个新列表,java,swing,user-interface,jcombobox,Java,Swing,User Interface,Jcombobox,我有以下代码,以便根据用户选择显示列表: private class SheepTypeListHandler implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { JComboBox<String> cb = (JComboBox<String>)(e.getSource()); if (cb.getSel

我有以下代码,以便根据用户选择显示列表:

    private class SheepTypeListHandler implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        JComboBox<String> cb = (JComboBox<String>)(e.getSource());
        if (cb.getSelectedItem().equals(sheepType[0])) {
            for (String item : eweClass )
            sheepClassCB.addItem(item);
        }
        else if (cb.getSelectedItem().equals(sheepType[1])) {
            for (String item : ramClass )
                sheepClassCB.addItem(item);
            for (String item : weight3 )
                weightCB.addItem(item);
        }
        else if (cb.getSelectedItem().equals(sheepType[2])) {
            for (String item : lambClass )
                sheepClassCB.addItem(item);
        }
    }
    } //End SheepTypeListHandler class
现在,如果他们改变主意,选择sheepType[0]而不是此列表:

Maintenance
Nonlactating, first 15 weeks gestation
Last 6 wks gestation OR Last 8 wks lactation suckling singles
First 8 wks lactation suckling singles OR last 8 wks lactation suckling twins
First 8 weeks lactation suckling twins
Replacement lambs and yearlings
我明白了:

Finishing
Early-weaned
Maintenance
Nonlactating, first 15 weeks gestation
Last 6 wks gestation OR Last 8 wks lactation suckling singles
First 8 wks lactation suckling singles OR last 8 wks lactation suckling twins
First 8 weeks lactation suckling twins
Replacement lambs and yearlings
如何更改当前代码以在进行新选择时覆盖,而不是仅添加到列表中? 谢谢

更新: 将我的部分代码编辑为以下内容:

private class SheepTypeListHandler implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        sheepClassCB.removeAllItems(); 
        if (sheepListCB.getSelectedItem().equals(sheepType[0])) {
            //sheepClassCB.removeAllItems(); //This did not work
            for (String item : eweClass ){
                sheepClassCB.addItem(item);
            }
        }
        else if (sheepListCB.getSelectedItem().equals(sheepType[1])) {
            for (String item : ramClass )
                sheepClassCB.addItem(item);
            for (String item : weight3 )
                weightCB.addItem(item);
        }
        else if (sheepListCB.getSelectedItem().equals(sheepType[2])) {
            for (String item : lambClass )
                sheepClassCB.addItem(item);
        }
    }
}

现在,当我第一次从列表中选择一个项目时,它就起作用了。但是,如果我重新选择一个不同的项目,它将显示一个空列表。

您似乎缺少sheepClassCB.removeAllItems行。我根据您的代码制作了一个简单的示例:

public class ListEmpty extends JFrame {

    JComboBox<String> changingCB = new JComboBox<>();
    JComboBox<String> unchangingCB = new JComboBox<>();

    String[] numbers = new String[] {"1", "2", "3"};
    String[] letters = new String[] {"a", "b", "c"};
    String[] symbols = new String[] {"!", "@", "#"};

    ListEmpty() {

        unchangingCB.addItem("Numbers");
        unchangingCB.addItem("Letters");
        unchangingCB.addItem("Symbols");
        unchangingCB.addActionListener(new SheepTypeListHandler());

        getContentPane().add(unchangingCB, BorderLayout.LINE_START);
        getContentPane().add(changingCB, BorderLayout.CENTER);

        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    private class SheepTypeListHandler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            changingCB.removeAllItems(); // Compare with removing this line.
            if (unchangingCB.getSelectedItem().equals("Numbers")) {
                for (String item : numbers)
                    changingCB.addItem(item);
            } else if (unchangingCB.getSelectedItem().equals("Letters")) {
                for (String item : letters)
                    changingCB.addItem(item);
            } else if (unchangingCB.getSelectedItem().equals("Symbols")) {
                for (String item : symbols)
                    changingCB.addItem(item);
            }
        }
    }

    public static void main(String[] args) {

        new ListEmpty();
    }
}

您可能想考虑一个Switter语句,而不是多个IF语句。

在添加数据之前清除列表。查看组件的API,看看它是否有用于此的方法。否则,请查看组件模型的API以了解方法是什么。sheepClassCB是什么类型?sheepClassCB是从字符串数组列表生成的JComboBox:JComboBox sheepListCB=new JComboxSheepType;查看我的答案,找出我的工作代码和你的工作代码之间的差异。上面的答案是有效的,但我发现如果多个组合框根据在另一个组合框中所做的不同选择而工作,那么你还需要在对一个组合框执行操作之前关闭ActionListener,然后在操作完成后重新打开它完成否则它仍然不能正常工作。@tinkerton101如果没有看到一个代码示例,说明多个组合框根据另一个组合框中的不同选择工作意味着什么,我就无法给出正确的响应。我在上面的回答是关于您最初的问题:当进行新选择时,我如何更改当前代码以覆盖,而不仅仅是添加到列表中?问题已经解决。添加代码修复了最初的问题,然后添加我的额外位在多个组合框必须相互交互的情况下起作用。@tinkerton101好的,但是打开和关闭ActionListeners通常意味着设计中出现了问题。不管什么对你有用。
public class ListEmpty extends JFrame {

    JComboBox<String> changingCB = new JComboBox<>();
    JComboBox<String> unchangingCB = new JComboBox<>();

    String[] numbers = new String[] {"1", "2", "3"};
    String[] letters = new String[] {"a", "b", "c"};
    String[] symbols = new String[] {"!", "@", "#"};

    ListEmpty() {

        unchangingCB.addItem("Numbers");
        unchangingCB.addItem("Letters");
        unchangingCB.addItem("Symbols");
        unchangingCB.addActionListener(new SheepTypeListHandler());

        getContentPane().add(unchangingCB, BorderLayout.LINE_START);
        getContentPane().add(changingCB, BorderLayout.CENTER);

        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    private class SheepTypeListHandler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            changingCB.removeAllItems(); // Compare with removing this line.
            if (unchangingCB.getSelectedItem().equals("Numbers")) {
                for (String item : numbers)
                    changingCB.addItem(item);
            } else if (unchangingCB.getSelectedItem().equals("Letters")) {
                for (String item : letters)
                    changingCB.addItem(item);
            } else if (unchangingCB.getSelectedItem().equals("Symbols")) {
                for (String item : symbols)
                    changingCB.addItem(item);
            }
        }
    }

    public static void main(String[] args) {

        new ListEmpty();
    }
}