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

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

问题描述

Hello我仍然是新手java希望学习这个不错的功能...
你好我有4组合框里面和里面是相同的

  -Select- 
项目1
项目2
项目3
项目4
上选择第1项时,会显示c>

c $ c>,
comboBox2,comboBox3和comboBox4 只有元素

  -Select- 
Item 2
Item 3
Item 4

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

  -Select- 
项目2
项目4

任何人都有想法如何在Java上这样做?我在Netbeans上使用GUI Builder ...



编辑1



是我的代码

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

并添加相同的代码 jComboBox2,jComboBox3然后...
当我去选择 -Select - -Select - 也走了...和



还有一个问题是当我已经选择了所有,并想着重新更改...所有项目都走了,没有更多的选择了。我只想再次回到可用的项目...



EDIT 2



b项目1
项目2< - 我选择Item2,然后另一个组合框将删除项目2 **
项目3
项目4

jComboBox2
-Select-
项目1
项目3 < - 然后我选择项目3
项目4

jComboBox3
-Select-
项目1
项目4 < - 然后项目4

jComboBox4
-Select-
项目1

但我改变主意...然后我需要回到 jComboBox2 选择 Item3
,因此我选择
jComboBox2 ,然后选择选择 - ,以便我在 jComboBox4

$ b $上选择 item3 b

但结果是
jComboBox4
null(无项目)

解决方案

不确定你的两个答案中哪一个将被删除,但这里有同样的答案。注意,你可以使用循环来创建所有的JComboBox和选项,以防止真正冗长的重复代码。然后你可以使用getSource()方法来告诉事件来自哪个组合框。如果你创建了你的JComboBoxes作为一个数组,你可以循环通过他们非常干净。为了添加东西回来我只是跟踪选择了什么,在哪个组合框使用String数组。然后,您可以检查此数组并使用它根据需要添加项目。请注意,他们不会以相同的顺序返回。如果你想要的功能,你可以使用insertItemAt,但这可能会得到一点凌乱(因为指数不断变化,因为你添加和删除项目),所以我把它留下了。

  //声明并初始化组合框中的选项
String [] options = {-Select-,Item 1 ,项目2,项目3,项目4};
//通过索引
//声明和初始化将在每个组合框中保存当前所选选项的数组例如,选择comboBoxes [1]的当前选定值[1]
String [] selected = {-Select-,-Select-,-Select-,-Select-};

//声明并初始化一个组合框数组。
//将创建四个组合框,其中包含选项数组
JComboBox [] comboBoxes = new JComboBox [4];
for(int i = 0; i comboBoxes [i] = new JComboBox(options);
}

私有无效jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt){
//循环遍历组合框中的所有组合框
for(int i = 0; i //检查数组中当前的组合框是否匹配你的事件源
if(evt.getSource()== comboBoxes [i ]){
//获取触发事件的组合框的字符串值
String currentSelection =(String)comboBoxes [i] .getSelectedItem();
//确保值实际改变
if(!currentSelection.equals(selected [i]){
//如果组合框的上一个值是-Select-don不要将它添加到所有其他组合框
if(!selected [i] .equals(options [0])){
//将上一个值添加回所有组合框事件
for(int j = 0; j if(j!= i){
comboBoxes [j] .addItem(selected [i] );
}
}
}
//如果组合框的当前值是-Select-,不要从所有其他组合框中删除
if !currentSelection.equals(options [0]){
//从除了触发事件的所有组合框中删除当前值
for(int j = 0; j < comboBoxes.length; j ++){
if(j!= i){
comboBoxes [j] .removeItem(comboBoxes [i] .getSelectedItem());
}
}
}
}
//将触发事件的组合框的选定项目设置为当前值
selected [i] = currentSelection;
}
}
}


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

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

-Select-
Item 2
Item 3
Item 4

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

-Select-
Item 2
Item 4

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

EDIT 1

this is my Code

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

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...

EDIT 2

Example

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 

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

but the result is jComboBox4 null (no items)

解决方案

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天全站免登陆