1

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

-Select-
Item 1
Item 2
Item 3
Item 4

当我选择Item 1on时comboBox1comboBox2,comboBox3 and comboBox4只有这些元素

-Select-
Item 2
Item 3
Item 4

然后当我选择Item 3comboBox2comboBox3 and comboBox4有这个剩余的元素

-Select-
Item 2
Item 4

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

编辑 1

这是我的代码

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

然后我添加了相同的代码......jComboBox2, jComboBox3 and jComboBox4当我去选择-Select--Select-它也消失了......并且

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

编辑 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-,所以我可以选择item3选择jComboBox4

但结果是 jComboBox4 null (没有项目)

4

1 回答 1

0

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

//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;
        }
    }
}
于 2013-09-23T00:04:28.997 回答