简体   繁体   English

如何将值加载到JComboBox中,具体取决于另一个JComboBox的选择?

[英]How to load values into JComboBox,depending on selection from another JComboBox?

Here is my code: 这是我的代码:

JComboBox unity=new JComboBox();
unity.setBounds(430,280,140,25);
unity.addItem("Pakistan");
unity.addItem("China");
unity.addItem("America");
unity.addItem("Japan");
unity.addItem("Bangladesh");
unity.addItem("Srilanka");
unity.addItem("India");
unity.addItem("Turkey");
unity.addItem("UK");
unity.addItem("Afghanistan");
unity.addItem("Iran");
unity.addItem("Iraq");
unity.setEditable(true);
uy.add(unity);

JLabel city=new JLabel("City:");
city.setBounds(350,320,100,25);
city.setForeground(Color.BLACK);
uy.add(city);
JComboBox cety=new JComboBox();
cety.setBounds(430,320,140,25);

cety.addItem("");
uy.add(cety);
unity.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent olala){
    if(unity.getSelectedItem().equals("Pakistan")){
        cety.addItem("Lahore");
        cety.addItem("Islamabad");
        cety.addItem("Karachi");
        cety.addItem("Rawalpindi");
        cety.addItem("Faisalabad");
        cety.addItem("Gujjranwala");
    }
}
});

But when i run the program,the block of if statement did not do anything. 但是当我运行程序时,if语句块没有做任何事情。 What should I do,if I want to Load Values into JComboBox , depending on selection from another JComboBox ? 如果我想将值加载到JComboBox ,取决于另一个JComboBox选择,我该怎么办?

Actually, if you choose "China" first, then "Pakistan" again, and click on the down arrow of the cety combobox, you'll see the items. 实际上,如果你首先选择“中国”,然后再选择“巴基斯坦”,然后点击cety组合框的向下箭头,你会看到这些项目。

You can easily debug and see when the method is called by adding a println like so, 您可以通过添加类似的println轻松调试并查看调用方法的时间,

        ...
        unity.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent olala) {
// DEBUG
System.out.println("itemStateChanged(): item = " + olala.getItem());
            ...

You'll notice that itemStateChanged is called when you select a different item. 你会发现, itemStateChanged当您选择不同的项目调用。 If you start the program and see the comboboxes for the first time, they haven't changed state yet, the method hasn't been called, and cety is empty. 如果你第一次启动程序并看到组合框,它们还没有改变状态,方法没有被调用,而cety是空的。 You could make unity change state, by adding 你可以通过添加来改变unity状态

        unity.setSelectedIndex(1);
        unity.setSelectedIndex(0);

at the end of your code. 在代码的最后。 Alternatively, you could just move all the unity.addItem lines to after the addItemListener so adding the first item to the list will trigger the event. 或者,您可以将所有unity.addItem行移动到addItemListener之后,因此将第一个项添加到列表将触发事件。

You'll need to clear the items before adding new ones, or selecting a couple of items in unity will result in a long list in cety . 你需要添加新的或选择在几个项目之前清除的项目unity将导致一个长长的清单cety If you need an empty string in the list, you need to move that statement here as well, 如果列表中需要空字符串,则还需要在此处移动该语句,

            ...
        public void itemStateChanged(ItemEvent olala){
            cety.removeAllItems();
            cety.addItem("");
            if (unity.getSelectedItem().equals("Pakistan")) {
                cety.addItem("Lahore");
                ...

You could then use a switch instead of a ton of if statements, get the item from the event instead of unity , and get rid of the raw types: 然后你可以使用一个switch而不是一大堆if语句,从事件而不是unity获取项目,并摆脱原始类型:

    JComboBox<String> unity = new JComboBox<>();
    unity.setBounds(430, 280, 140, 25);
    unity.setEditable(true);
    uy.add(unity);

    JLabel city = new JLabel("City:");
    city.setBounds(350, 320, 100, 25);
    city.setForeground(Color.BLACK);
    uy.add(city);

    JComboBox<String> cety = new JComboBox<>();
    cety.setBounds(430, 320, 140, 25);
    uy.add(cety);

    unity.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent event) {
            cety.removeAllItems();
            cety.addItem("");
            switch (event.getItem().toString()) {
                case "Pakistan":
                    cety.addItem("Lahore");
                    cety.addItem("Islamabad");
                    cety.addItem("Karachi");
                    cety.addItem("Rawalpindi");
                    cety.addItem("Faisalabad");
                    cety.addItem("Gujjranwala");
                    break;
                case "China":
                    cety.addItem("Beijing");
                    break;
            //  ...
            }
        }
    });
    unity.addItem("Pakistan");
    unity.addItem("China");
    unity.addItem("America");
    unity.addItem("Japan");
    unity.addItem("Bangladesh");
    unity.addItem("Srilanka");
    unity.addItem("India");
    unity.addItem("Turkey");
    unity.addItem("UK");
    unity.addItem("Afghanistan");
    unity.addItem("Iran");
    unity.addItem("Iraq");

// This would be a workaround if you don't want to move the addItems:
//  unity.setSelectedIndex(1);
//  unity.setSelectedIndex(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM