英文:
How to reorganize JCombobox depends on prior choice (Java Swing)
问题
JPanel test = new JPanel();
test.setLayout(new FlowLayout());
add(test);
top = rc.getParsedData("top");
ArrayList<String> topList = rc.getLocationList(top);
location1 = new JComboBox(topList.toArray(new String[topList.size()]));
location1.addActionListener(e -> {
try {
rc.setCode(top, location1.getSelectedItem().toString());
mdl = rc.getParsedData("mdl");
ArrayList<String> mdlList = rc.getLocationList(mdl);
location2 = new JComboBox(mdlList.toArray(new String[mdlList.size()]));
} catch (IOException | ParseException ioException) {
ioException.printStackTrace();
}
});
test.add(location1);
location2.addActionListener(e -> {
try {
leaf = rc.getParsedData("leaf");
rc.setCode(mdl, location2.getSelectedItem().toString());
ArrayList<String> leafList = rc.getLocationList(leaf);
location3 = new JComboBox(leafList.toArray(new String[leafList.size()]));
rc.setXY(leaf, location3.getSelectedItem().toString());
} catch (IOException | ParseException ioException) {
ioException.printStackTrace();
}
});
test.add(location2);
test.add(location3);
英文:
I'm about to make selection menus consist of 3 JComboboxes, and they are related to prior choice.<br>
When I choose first menu, I need to update next JCombobox by requesting JSON, and so on for last menu.<br>
What I tried : put addActionListener to update next JCombobox, but it seems not working.<br>
It's hard to find problem as I cannot catch it through debugger.<br>
The method 'getParsedData' returns JSONArray came from JSON file.
JPanel test = new JPanel();
test.setLayout(new FlowLayout());
add(test);
top = rc.getParsedData("top");
ArrayList<String> topList = rc.getLocationList(top);
location1 = new JComboBox(topList.toArray(new String[topList.size()]));
location1.addActionListener(e -> {
try {
rc.setCode(top, location1.getSelectedItem().toString());
mdl = rc.getParsedData("mdl");
ArrayList<String> mdlList = rc.getLocationList(mdl);
location2 = new JComboBox(mdlList.toArray(new String[mdlList.size()]));
} catch (IOException | ParseException ioException) {
ioException.printStackTrace();
}
});
test.add(location1);
location2.addActionListener(e -> {
try {
leaf = rc.getParsedData("leaf");
rc.setCode(mdl, location2.getSelectedItem().toString());
ArrayList<String> leafList = rc.getLocationList(leaf);
location3 = new JComboBox(leafList.toArray(new String[leafList.size()]));
rc.setXY(leaf, location3.getSelectedItem().toString());
} catch (IOException | ParseException ioException) {
ioException.printStackTrace();
}
});
test.add(location2);
test.add(location3);
答案1
得分: 1
不要回答我要翻译的问题。以下是要翻译的内容:
每当ActionListener被激活时,不要创建一个新的JComboBox(使用new JComboBox(...)
);而应该更新现有的location2/location3实例。
对于location2,您可以通过首先调用location2.removeAllItems()
来实现。然后,您应该遍历您的mdList,并对每个mdList项目调用location2.addItem()
。
英文:
Instead of creating a new JComboBox each time the ActionListener gets active (using <code>new JComboBox(...))</code> you should update the existing location2/location3-instances.
For location2 you can do that by first calling
<code>location2.removeAllItems()</code>. Afterwards you should iterate over your mdList and call <code>location2.addItem()</code> for each mdList-item.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论