您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Java-如果在combox1中选择了一个值,则应在所有其他组合框中将其禁用

Java-如果在combox1中选择了一个值,则应在所有其他组合框中将其禁用

不知道您的两个答案中的哪个将被删除,但是这里又是相同的答案。请注意,您可以使用循环创建所有JCombo@R_272_2419@es和选项,以防止真正冗长的重复代码。然后,您可以使用getSource()方法来判断事件来自哪个组合框。如果将JCombo@R_272_2419@es创建为数组,则可以非常清晰地循环遍历它们。为了重新添加内容,我只需要跟踪已选择的内容以及使用String数组的组合框。然后,您可以检查此数组,并根据需要使用它来添加项目。请注意,它们不会以相同的顺序返回。如果您想要该功能,则可以使用insertItemAt,但这可能会有些混乱(因为自从添加删除项以来索引一直在变化),因此我将其省略。

//Declare and initialize the options that the combo@R_272_2419@es 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 combo@R_272_2419@ by index
//For example the currently selected value of combo@R_272_2419@es[1] is selected[1]
String[] selected = {"-Select-", "-Select-", "-Select-", "-Select-"};

//Declare and initialize an array of combo@R_272_2419@es. 
//Four combo@R_272_2419@es will be created all containing the options array
JCombo@R_272_2419@[] combo@R_272_2419@es = new JCombo@R_272_2419@[4];
for(int i = 0; i < combo@R_272_2419@.length; i++) {
    combo@R_272_2419@es[i] = new JCombo@R_272_2419@(options);
}

private void jCombo@R_272_2419@1ItemStateChanged(java.awt.event.ItemEvent evt) {
    //Loop through all of the combo@R_272_2419@es in combo@R_272_2419@es
    for(int i = 0; i < combo@R_272_2419@es.length; i++) {
        //Check to see if the current combo@R_272_2419@ in the array matches the source of your event
        if(evt.getSource() == combo@R_272_2419@es[i]) {
            //Get the string value of the combo@R_272_2419@ that fired the event
            String currentSelection = (String)combo@R_272_2419@es[i].getSelectedItem();
            //Make sure that the value actually changed
            if(!currentSelection.equals(selected[i]) {
                //If the prevIoUs value of the combo@R_272_2419@ was "-Select-" don't add it to all the other combo@R_272_2419@es
                if(!selected[i].equals(options[0])) {
                    //Add back the prevIoUs value to all combo@R_272_2419@es other than the one that fired the event
                    for(int j = 0; j < combo@R_272_2419@es.length; j++) {
                        if(j != i) {
                            combo@R_272_2419@es[j].addItem(selected[i]);
                        }
                    }
                }
                //If current value of the combo@R_272_2419@ is "-Select-" don't remove it from all other combo@R_272_2419@es
                if(!currentSelection.equals(options[0]) {
                    //Remove the current value from all combo@R_272_2419@es other than the one that fired the event
                    for(int j = 0; j < combo@R_272_2419@es.length; j++) {
                        if(j != i) {
                            combo@R_272_2419@es[j].removeItem(combo@R_272_2419@es[i].getSelectedItem());
                        }
                    }
                }
            }
            //Set the selected item for the combo@R_272_2419@ that fired the event to the current value
            selected[i] = currentSelection;
        }
    }
}
java 2022/1/1 18:25:53 有305人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶