Java 如果在jComBox1中选择了某个项,则应在所有其他组合框中禁用该项

Java 如果在jComBox1中选择了某个项,则应在所有其他组合框中禁用该项,java,swing,jcombobox,Java,Swing,Jcombobox,你好,我还是java的新手,希望学习这个好功能。。。 你好,我有4个组合框,里面和里面是一样的 -Select- Item 1 Item 2 Item 3 Item 4 当我在组合框1上选择项目1时, comboBox2、comboBox3和comboBox4只有这些元素 -Select- Item 2 Item 3 Item 4 然后,当我在comboBox2上选择Item 3时,comboBox3和comboBox4有这个剩余元素 -Select- Item 2 Item 4 有人知道

你好,我还是java的新手,希望学习这个好功能。。。 你好,我有4个组合框,里面和里面是一样的

-Select-
Item 1
Item 2
Item 3
Item 4
当我在
组合框1
上选择
项目1
时,
comboBox2、comboBox3和comboBox4
只有这些元素

-Select-
Item 2
Item 3
Item 4
然后,当我在
comboBox2
上选择
Item 3
时,
comboBox3和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());
}
在这之后,我添加了相同的代码jcombox2、jcombox3和jcombox4。。。 当我去选择
-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 
jcombox1
-挑选-
项目1

条目2当某些组合框的状态发生变化时,您不仅应该从其他组合框中删除条目,还应该插入。 例如,如果选择了item1,然后决定选择item3,则必须从所有其他组合框中删除item3并插入item1。

您可以使用某种“代理”模型来过滤各个组合框

您可以从一个包含所有可用项的主组合框模型开始,而不是尝试从不同的组合框中添加和删除项

然后,每个组合框都会有自己的“代理”模型(使用主模型作为基础),该模型能够从组合框使用的列表中“过滤”项目

这样,您只需告诉“代理”模型要过滤掉哪些项,并允许底层API处理其余项。

要解决“选择”问题,您可以验证所选索引是否与0不同(假设“选择”选项始终是第一个)


每次更改组合时,都需要删除其他组合中的选定项,并在其他组合中添加未选定项

您可以更干净地使用循环创建所有这些框和选项。此代码未经测试,但应该可以工作

//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;
        }
    }
}
//声明并初始化组合框将具有的选项
字符串[]选项={“-Select-”,“项目1”,“项目2”,“项目3”,“项目4”};
//声明并初始化一个数组,该数组将按索引保存每个组合框中当前选定的选项
//例如,选择组合框[1]的当前选定值[1]
字符串[]selected={“-Select-”,“-Select-”,“-Select-”,“-Select-”};
//声明并初始化组合框数组。
//将创建四个组合框,所有组合框都包含选项数组
JComboBox[]组合框=新的JComboBox[4];
for(int i=0;i
看起来像一个允许选择多个项目的单一
JList
,对最终用户来说会更容易,也不会那么混乱。对我来说,这个“不错的功能”就像是“正在制作中的无法使用的GUI”。