0

I'm about to make selection menus consist of 3 JComboboxes, and they are related to prior choice.
When I choose first menu, I need to update next JCombobox by requesting JSON, and so on for last menu.
What I tried : put addActionListener to update next JCombobox, but it seems not working.
It's hard to find problem as I cannot catch it through debugger.
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);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard code data for the combo boxes & first work out the details with ***two*** combo boxes. – Andrew Thompson Aug 25 '20 at 06:17
  • The first menu contains information about 'state/province', and next menu prints list about 'city' that belongs to prior one. – cheerfulbear Aug 25 '20 at 06:37
  • Java Swing, as well as most other GUI libraries, is built with the assumption that you will create the GUI components one time. After all the GUI components are created, you can update the **contents** (text, image) of the GUI components. Adding and removing GUI components from a displayed GUI is an advanced GUI topic that has to be done carefully. – Gilbert Le Blanc Aug 25 '20 at 11:02
  • Check out: https://stackoverflow.com/questions/4982260/binding-comboboxes-in-swing/4982576#4982576 for a possible solution. – camickr Aug 25 '20 at 14:22

1 Answers1

1

Instead of creating a new JComboBox each time the ActionListener gets active (using new JComboBox(...)) you should update the existing location2/location3-instances.

For location2 you can do that by first calling location2.removeAllItems(). Afterwards you should iterate over your mdList and call location2.addItem() for each mdList-item.

MaS
  • 393
  • 3
  • 17