JComboBox Select和Item以及基于选择的显示新列表

时间:2015-04-24 20:04:49

标签: 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.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 [2],则会出现以下列表:

Finishing
Early-weaned

现在,如果他们改变主意并选择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);
        }
    }
}

现在我第一次从列表中选择一个项目。但如果我重新选择一个不同的项目,它会显示一个空列表。

1 个答案:

答案 0 :(得分:0)

您似乎错过了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();
    }
}

您可能需要考虑switch语句而不是多个if-else语句。