-2

您好,我还是 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

4 回答 4

0

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

每次更改组合时,您都需要删除其他组合上的选定项目并添加其他组合上的未选定项目。

于 2013-09-22T23:41:29.043 回答
0

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

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

然后,每个组合框将有自己的“代理”模型(使用主模型作为基础),然后可以从组合框使用的列表中“过滤”项目。

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

于 2013-09-22T23:38:28.427 回答
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;
        }
    }
}
于 2013-09-22T23:59:35.520 回答
0

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

于 2013-09-22T23:12:04.707 回答