Java - 如果在 combox1 中选择了一个值,那么它应该在所有其他组合框中禁用 [英] Java - if a value selected in combox1 then it should be disable in all other combo boxes

查看:22
本文介绍了Java - 如果在 combox1 中选择了一个值,那么它应该在所有其他组合框中禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我还是 Java 新手,希望能学习到这个不错的功能...您好,我有 4 个组合框,它们的内部和内部都相同

Hello I'm still newbie on java hope to learn this nice feature... Hello i have 4 combo box that have the same inside and inside of it is

-Select-
Item 1
Item 2
Item 3
Item 4

当我在 comboBox1 上选择 Item 1 时,comboBox2,comboBox3 和 comboBox4 只有这些元素

and when I choose Item 1 on comboBox1, the comboBox2,comboBox3 and comboBox4 have elements only these

-Select-
Item 2
Item 3
Item 4

然后当我在 comboBox2 上选择 Item 3 时,comboBox3 和 comboBox4 有这个剩余元素

and then when I choose Item 3 on comboBox2, the comboBox3 and comboBox4 have this leftover elements

-Select-
Item 2
Item 4

有人知道如何在 Java 上做到这一点吗?我在 Netbeans 上使用 GUI Builder...

anyone have idea how to do this on Java? I am using GUI Builder on Netbeans...

编辑 1

这是我的代码

private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {
    jComboBox2.removeItem(jComboBox1.getSelectedItem());
    jComboBox3.removeItem(jComboBox1.getSelectedItem());
    jComboBox4.removeItem(jComboBox1.getSelectedItem());
}

然后我添加相同的代码 jComboBox2, jComboBox3 和 jComboBox4 之后......当我选择 -Select- 时, -Select- 也消失了......和 ​​

and I add on the same code jComboBox2, jComboBox3 and jComboBox4 after that... when I go choose -Select- the -Select- is gone too... and

还有一个问题是当我已经选择了所有并想再次更改它时......所有项目都消失了,不再有更多选择......我只想再次支持可用项目......

one more problem is when I already choose all and thinking to rechange it again... all items are gone and no more choices anymore.. I just want to back the available items again...

编辑 2

示例

jComboBox1
-Select-
Item 1
Item 2 <-- I select Item2, then the other combo box will remove Item 2**
Item 3
Item 4

jComboBox2
-Select-
Item 1
Item 3 <-- then I select Item 3
Item 4

jComboBox3
-Select-
Item 1
Item 4 <-- then Item 4

jComboBox4
-Select-
Item 1 

但我改变主意了...然后我需要回到 jComboBox2 选择 Item3所以我选择jComboBox2 并选择 -Select-,这样我就可以在 jComboBox4

but i'm changing my mind... then I need to go back to jComboBox2 to select Item3 so I choose jComboBox2 and select -Select-, so I can select item3 on jComboBox4

但结果是jComboBox4null(没有项目)

but the result is jComboBox4 null (no items)

推荐答案

不确定您的两个答案中的哪一个会被删除,但这里又是相同的答案.请注意,您可以使用循环来创建所有 JComboBox 和选项,以防止真正冗长的重复代码.然后您可以使用 getSource() 方法来判断事件来自哪个组合框.如果您将 JComboBoxes 创建为数组,则可以非常干净地循环遍历它们.为了将内容添加回来,我将使用 String 数组跟踪已选择的内容以及在哪个组合框中.然后,您可以检查此数组并使用它根据需要重新添加项目.请注意,它们不会以相同的顺序返回.如果您想要该功能,您可以使用 insertItemAt,但这可能会有点混乱(因为在您添加和删除项目后索引会不断变化),所以我将其排除在外.

Not sure which of your two answers will be deleted, but here's the same answer again. Note that you can create all of your JComboBoxes and options using loops to prevent really lengthy repetitive code. Then you can use the getSource() method to tell which combobox the event came from. If you created your JComboBoxes as an array you can loop through them very cleanly. In order to add things back in I would just keep track of what has been selected and in which combobox using a String array. You can then check this array and use it to add items back in as needed. Note that they won't go back in the same order. If you want that functionality you can play around with insertItemAt, but that would probably get a little messy (since the indices are constantly changing since you're adding and removing items) so I've left it out.

//Declare and initialize the options that the comboboxes will have
String[] options = {"-Select-", "Item 1", "Item 2", "Item 3", "Item 4"};
//Declare and initialize an array that will hold the currently selected options in each combobox by index
//For example the currently selected value of comboBoxes[1] is selected[1]
String[] selected = {"-Select-", "-Select-", "-Select-", "-Select-"};

//Declare and initialize an array of comboBoxes. 
//Four comboboxes will be created all containing the options array
JComboBox[] comboBoxes = new JComboBox[4];
for(int i = 0; i < comboBox.length; i++) {
    comboBoxes[i] = new JComboBox(options);
}

private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {
    //Loop through all of the comboboxes in comboBoxes
    for(int i = 0; i < comboBoxes.length; i++) {
        //Check to see if the current combobox in the array matches the source of your event
        if(evt.getSource() == comboBoxes[i]) {
            //Get the string value of the combobox that fired the event
            String currentSelection = (String)comboBoxes[i].getSelectedItem();
            //Make sure that the value actually changed
            if(!currentSelection.equals(selected[i]) {
                //If the previous value of the combobox was "-Select-" don't add it to all the other comboboxes
                if(!selected[i].equals(options[0])) {
                    //Add back the previous value to all comboboxes other than the one that fired the event
                    for(int j = 0; j < comboBoxes.length; j++) {
                        if(j != i) {
                            comboBoxes[j].addItem(selected[i]);
                        }
                    }
                }
                //If current value of the combobox is "-Select-" don't remove it from all other comboboxes
                if(!currentSelection.equals(options[0]) {
                    //Remove the current value from all comboboxes other than the one that fired the event
                    for(int j = 0; j < comboBoxes.length; j++) {
                        if(j != i) {
                            comboBoxes[j].removeItem(comboBoxes[i].getSelectedItem());
                        }
                    }
                }
            }
            //Set the selected item for the combobox that fired the event to the current value
            selected[i] = currentSelection;
        }
    }
}

这篇关于Java - 如果在 combox1 中选择了一个值,那么它应该在所有其他组合框中禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆