如何根据先前的选择重新组织 JCombobox(Java Swing)

huangapple go评论103阅读模式
英文:

How to reorganize JCombobox depends on prior choice (Java Swing)

问题

  1. JPanel test = new JPanel();
  2. test.setLayout(new FlowLayout());
  3. add(test);
  4. top = rc.getParsedData("top");
  5. ArrayList<String> topList = rc.getLocationList(top);
  6. location1 = new JComboBox(topList.toArray(new String[topList.size()]));
  7. location1.addActionListener(e -> {
  8. try {
  9. rc.setCode(top, location1.getSelectedItem().toString());
  10. mdl = rc.getParsedData("mdl");
  11. ArrayList<String> mdlList = rc.getLocationList(mdl);
  12. location2 = new JComboBox(mdlList.toArray(new String[mdlList.size()]));
  13. } catch (IOException | ParseException ioException) {
  14. ioException.printStackTrace();
  15. }
  16. });
  17. test.add(location1);
  18. location2.addActionListener(e -> {
  19. try {
  20. leaf = rc.getParsedData("leaf");
  21. rc.setCode(mdl, location2.getSelectedItem().toString());
  22. ArrayList<String> leafList = rc.getLocationList(leaf);
  23. location3 = new JComboBox(leafList.toArray(new String[leafList.size()]));
  24. rc.setXY(leaf, location3.getSelectedItem().toString());
  25. } catch (IOException | ParseException ioException) {
  26. ioException.printStackTrace();
  27. }
  28. });
  29. test.add(location2);
  30. 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.

  1. JPanel test = new JPanel();
  2. test.setLayout(new FlowLayout());
  3. add(test);
  4. top = rc.getParsedData(&quot;top&quot;);
  5. ArrayList&lt;String&gt; topList = rc.getLocationList(top);
  6. location1 = new JComboBox(topList.toArray(new String[topList.size()]));
  7. location1.addActionListener(e -&gt; {
  8. try {
  9. rc.setCode(top, location1.getSelectedItem().toString());
  10. mdl = rc.getParsedData(&quot;mdl&quot;);
  11. ArrayList&lt;String&gt; mdlList = rc.getLocationList(mdl);
  12. location2 = new JComboBox(mdlList.toArray(new String[mdlList.size()]));
  13. } catch (IOException | ParseException ioException) {
  14. ioException.printStackTrace();
  15. }
  16. });
  17. test.add(location1);
  18. location2.addActionListener(e -&gt; {
  19. try {
  20. leaf = rc.getParsedData(&quot;leaf&quot;);
  21. rc.setCode(mdl, location2.getSelectedItem().toString());
  22. ArrayList&lt;String&gt; leafList = rc.getLocationList(leaf);
  23. location3 = new JComboBox(leafList.toArray(new String[leafList.size()]));
  24. rc.setXY(leaf, location3.getSelectedItem().toString());
  25. } catch (IOException | ParseException ioException) {
  26. ioException.printStackTrace();
  27. }
  28. });
  29. test.add(location2);
  30. 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.

huangapple
  • 本文由 发表于 2020年8月25日 13:58:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63572840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定