Java – 如果在combox1中选择了一个值,那么它应该在所有其他combobox中禁用

你好我仍然是java上的新手希望学习这个很好的function…你好我有4个combobox,内部和内部相同是

-Select- Item 1 Item 2 Item 3 Item 4 

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

 -Select- Item 2 Item 3 Item 4 

然后当我在comboBox2上选择Item 3时, comboBox3 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并选择jComboBox2 ,所以我可以在jComboBox4上选择jComboBox4

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

不确定你的两个答案中的哪一个会被删除,但这里的答案又是一样的。 请注意,您可以使用循环创建所有JComboBox和选项,以防止冗长的重复代码。 然后你可以使用getSource()方法来告诉事件来自哪个combobox。 如果您将JComboBoxes创建为数组,则可以非常干净地遍历它们。 为了重新添加内容,我只需跟踪已选择的内容以及使用String数组的combobox。 然后,您可以检查此arrays并使用它根据需要添加项目。 请注意,它们不会以相同的顺序返回。 如果你想要这个function你可以使用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; } } }