Java-如果在combox1中选择了一个值,则应在所有其他组合框中将其禁用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java-如果在combox1中选择了一个值,则应在所有其他组合框中将其禁用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3651字,纯文字阅读大概需要6分钟。
内容图文
![Java-如果在combox1中选择了一个值,则应在所有其他组合框中将其禁用](/upload/InfoBanner/zyjiaocheng/690/1dec345cf6c04e4bb8484ff5e04a8813.jpg)
您好,我还是Java的新手,希望学习这个不错的功能…
您好,我有4个组合框,里面和里面的相同
-Select-
Item 1
Item 2
Item 3
Item 4
当我在comboBox1上选择项目1时,
comboBox2,comboBox3和comboBox4仅具有这些元素
-Select-
Item 2
Item 3
Item 4
然后当我在comboBox2上选择第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());
}
在那之后我添加了相同的代码jComboBox2,jComboBox3和jComboBox4 …
当我选择-选择-时-选择-也消失了…
还有一个问题是,当我已经选择全部并且想再次更改它时…所有项目都消失了,不再有其他选择了..我只想再次备份可用项目…
编辑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-,因此我可以在jComboBox4上选择item3
但结果是
jComboBox4
空(无项目)
解决方法:
不知道您的两个答案中的哪个将被删除,但这又是相同的答案.请注意,您可以使用循环创建所有JComboBoxes和选项,以防止真正冗长的重复代码.然后,您可以使用getSource()方法来判断事件来自哪个组合框.如果将JComboBoxes创建为数组,则可以非常清晰地循环遍历它们.为了重新添加内容,我只需要跟踪选择了什么以及使用String数组的组合框.然后,您可以检查此数组,并根据需要使用它来添加项目.请注意,它们不会以相同的顺序返回.如果您想要该功能,则可以使用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;
}
}
}
内容总结
以上是互联网集市为您收集整理的Java-如果在combox1中选择了一个值,则应在所有其他组合框中将其禁用全部内容,希望文章能够帮你解决Java-如果在combox1中选择了一个值,则应在所有其他组合框中将其禁用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。