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

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

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(&quot;top&quot;);
        ArrayList&lt;String&gt; topList = rc.getLocationList(top);
        location1 = new JComboBox(topList.toArray(new String[topList.size()]));
        location1.addActionListener(e -&gt; {
            try {
                rc.setCode(top, location1.getSelectedItem().toString());
                mdl = rc.getParsedData(&quot;mdl&quot;);
                ArrayList&lt;String&gt; 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 -&gt; {
            try {
                leaf = rc.getParsedData(&quot;leaf&quot;);
                rc.setCode(mdl, location2.getSelectedItem().toString());
                ArrayList&lt;String&gt; 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.

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:

确定