在为 JComboBox 选择一个值后,JComboBox 的另一个(或其余)将从前一个中删除该选定值
After selecting a value to a JComboBox, the other(or the rest) of the JComboBox will delete that selected value from the previous one
我一直在制作一个程序,它会要求用户在 JTextField 中输入要添加的部门数量。单击输入后,它将通过一个 for 循环,然后创建与您输入的金额相同的金额。
在制作程序时,我想到了我想制作它的地方,一旦您在第一个 JComboBox 中选择了一个项目,其余的 JComboBox 将不再有您在前一个。但是,我似乎不明白我将如何编码。
例如,JComboBox A、B 和 C 有项目:"Fire", "Water" and "Wind"
。
一旦您为 A 选择了项目:"Fire"
,JComboBox B 和 C 应该在他们的选择中留下项目:"Water" and "Wind"
。最后,在 B 上选择项目:"Wind"
将具有剩余项目:"Water"
到 C.
在我创建的程序中,此按钮将在从 TextField 输入数字后创建 JComboBoxes:
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();
}
}
我将如何解决我面临的问题?将不胜感激,谢谢!
没关系,我已经通过从 JComboBox 获取索引并将初始索引添加 1 并循环让另一个 JComboBox 以相同的方式执行此操作来解决此问题
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");
cb.setName("dept"+i);
dept(cb);
cb.addItemListener(deletePrevious);
listCB.add(cb);
deptContent.add(cb);
}
revalidate();
repaint();
}
});
deletePrevious
ItemListener:
private ItemListener deletePrevious = new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
JComboBox<?> source = (JComboBox<?>) e.getSource();
int obtainedNum = Integer.valueOf(source.getName().substring(4)) + 1;
String obtainedItem = source.getSelectedItem().toString();
if(obtainedNum < listCB.size()) {
while(obtainedNum < listCB.size()) {
if(source.getSelectedIndex() > 0) {
listCB.get(obtainedNum).removeItem(obtainedItem);
}
obtainedNum++;
}
}
counter = 0;
revalidate();
repaint();
} else {
}
}
};
但是,仍然存在问题,例如:如果您更改第一个JComboBox,它将从第一个JComboBox中删除选定的值,但是尽管如此,我已经用我不完善的解决方案解决了我的问题。
我一直在制作一个程序,它会要求用户在 JTextField 中输入要添加的部门数量。单击输入后,它将通过一个 for 循环,然后创建与您输入的金额相同的金额。
在制作程序时,我想到了我想制作它的地方,一旦您在第一个 JComboBox 中选择了一个项目,其余的 JComboBox 将不再有您在前一个。但是,我似乎不明白我将如何编码。
例如,JComboBox A、B 和 C 有项目:"Fire", "Water" and "Wind"
。
一旦您为 A 选择了项目:"Fire"
,JComboBox B 和 C 应该在他们的选择中留下项目:"Water" and "Wind"
。最后,在 B 上选择项目:"Wind"
将具有剩余项目:"Water"
到 C.
在我创建的程序中,此按钮将在从 TextField 输入数字后创建 JComboBoxes:
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();
}
}
我将如何解决我面临的问题?将不胜感激,谢谢!
没关系,我已经通过从 JComboBox 获取索引并将初始索引添加 1 并循环让另一个 JComboBox 以相同的方式执行此操作来解决此问题
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");
cb.setName("dept"+i);
dept(cb);
cb.addItemListener(deletePrevious);
listCB.add(cb);
deptContent.add(cb);
}
revalidate();
repaint();
}
});
deletePrevious
ItemListener:
private ItemListener deletePrevious = new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
JComboBox<?> source = (JComboBox<?>) e.getSource();
int obtainedNum = Integer.valueOf(source.getName().substring(4)) + 1;
String obtainedItem = source.getSelectedItem().toString();
if(obtainedNum < listCB.size()) {
while(obtainedNum < listCB.size()) {
if(source.getSelectedIndex() > 0) {
listCB.get(obtainedNum).removeItem(obtainedItem);
}
obtainedNum++;
}
}
counter = 0;
revalidate();
repaint();
} else {
}
}
};
但是,仍然存在问题,例如:如果您更改第一个JComboBox,它将从第一个JComboBox中删除选定的值,但是尽管如此,我已经用我不完善的解决方案解决了我的问题。