在选择Jcombobox的值之后,Jcombobox的另一个(或其余)将从上一个删除该值
我一直在制作一个程序,它将要求用户在JTEXTFIELD中输入它将添加多少个部门。单击输入后,它将通过循环,然后创建您输入的相同数量。
在制作程序时,我想到了我想在哪里进行的,一旦您选择了第一个Jcombobox的项目,其余的Jcomboboxes将不再有您在上一篇中选择的项目。但是,我似乎不明白我将如何编码。
例如,jcombobox a,b和c有项目:“ fire”,“ water”和“ wind”
。
选择该项目后:为A,Jcombobox B和C的“ fire”
应该具有:“水”和“ wind”和“ wind”
他们选择。最后,选择项目:B上的“ wind”
将具有其余项目:“ WATER”
到C。
从我创建的程序中,此按钮将创建Jcomboboxes从Textfield输入一个数字之后:
JButton enterButton = new JButton("Enter");
enterButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
listCB.clear();
deptContent.removeAll();
int count = Integer.valueOf(textField.getText());
for(int i = 0; i < count; i++) {
JComboBox<String> cb = new JComboBox<String>();
cb.addItem("Select a department");
dept(cb); // this function will add the items in cb
cb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
if(cb.getSelectedIndex() > 0) {
selectedDept = cb.getSelectedItem().toString();
obtainedDeptNames.add(selectedDept);
} else {
obtainedDeptNames.remove(selectedDept);
}
revalidate();
repaint();
}
}
});
listCB.add(cb);
deptContent.add(cb);
}
revalidate();
repaint();
}
});
这是dept()
函数:
private void dept(JComboBox<String> cb) {
try (Connection conn = DriverManager.getConnection(MySQLConnectivity.URL, MySQLConnectivity.user ,MySQLConnectivity.pass)){
PreparedStatement getStatement = conn.prepareStatement("select departmentname from departmentinfo where schoolname='"+obtainedSchoolName+"'");
ResultSet result = getStatement.executeQuery();
while(result.next()) {
String obtainedDept = result.getString("departmentname");
cb.addItem(obtainedDept);
}
} catch (SQLException sql) {
sql.printStackTrace();
}
}
我将如何处理我面临的问题?答案将不胜感激,谢谢!
I've been making a program where it will ask the user to input in the JTextField how many Departments it will add. Once clicked enter, it will go through a for-loop which then will create the same amount you've entered.
While making the program, I've thought of where I wanted to make it wherein once you selected an item to the first JComboBox, the rest of the JComboBoxes will have no more of that item you've selected in the previous one. However, I don't seem to understand how will I code it.
For instance, JComboBox A, B and, C have items: "Fire", "Water" and "Wind"
.
Once you've selected the item: "Fire"
for A, JComboBox B and C should have items: "Water" and "Wind"
left on their selection. Lastly, selecting item: "Wind"
on B will have the remaining item: "Water"
to C.
From the program I've created, this button will create the JComboBoxes after inputting a number from the TextField:
JButton enterButton = new JButton("Enter");
enterButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
listCB.clear();
deptContent.removeAll();
int count = Integer.valueOf(textField.getText());
for(int i = 0; i < count; i++) {
JComboBox<String> cb = new JComboBox<String>();
cb.addItem("Select a department");
dept(cb); // this function will add the items in cb
cb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
if(cb.getSelectedIndex() > 0) {
selectedDept = cb.getSelectedItem().toString();
obtainedDeptNames.add(selectedDept);
} else {
obtainedDeptNames.remove(selectedDept);
}
revalidate();
repaint();
}
}
});
listCB.add(cb);
deptContent.add(cb);
}
revalidate();
repaint();
}
});
Here's the dept()
function:
private void dept(JComboBox<String> cb) {
try (Connection conn = DriverManager.getConnection(MySQLConnectivity.URL, MySQLConnectivity.user ,MySQLConnectivity.pass)){
PreparedStatement getStatement = conn.prepareStatement("select departmentname from departmentinfo where schoolname='"+obtainedSchoolName+"'");
ResultSet result = getStatement.executeQuery();
while(result.next()) {
String obtainedDept = result.getString("departmentname");
cb.addItem(obtainedDept);
}
} catch (SQLException sql) {
sql.printStackTrace();
}
}
How will I do the problem I'm facing? Answers will be appreciated, thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系,我已经通过从jcombobox获取索引并将初始索引添加为1,并循环以其他Jcombobox的方式添加初始索引,以相同的方式
deleteprevious
itemListener:但是,仍然存在一些问题,例如:如果您更改了第一个JCombobox,它将从第一个Jcombobox删除所选值,但是我已经通过不完美的解决方案解决了问题的问题。
Nevermind, I've solved it by getting the index from the JComboBox and add the initial index by 1 and loop for the other JComboBox to do the same way
deletePrevious
ItemListener:However, there are still problems such as: if you change the first JComboBox, it will delete the selected value from the first JComboBox but nonetheless, I've solved the problem of my question with my imperfect solution.