0

I have a program which have 3 JComboBoxes, the there are selectBuilding, selectFloor and selectRoom. When the selectBuilding has an item selected, an image of a tick should be displayed next to it and the selectFloor ComboBox will be pouplated. The same thing with selectFloor and selectRoom

I have gotten the populating dropdown menus to work, however the ticks are not showing up in the correct order: When i choose an item from selectBuilding, no tick shows up next to it. But then I choose an item from selectFloor and both ticks show up. Why is that?

here's my code for selectBuilding and selectFloor JComboBoxes:

JComboBox selectBuilding = new JComboBox(buildingModel); //Creates dropdown menu
selectBuilding.setBounds(46, 60, 150, 40);
frame.getContentPane().add(selectBuilding);
//adds actionlistner
selectBuilding.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            buildingSelected = true; //Boolean values to determine whether each dropdown has been selected
            floorSelected = false;
            roomSelected = false;
            if(buildingSelected == true) {
                BufferedImage tick = null;
                try {
                    tick = ImageIO.read(new File("src/images/tick.png"));
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                buildingTick = new JLabel(new ImageIcon(new ImageIcon(tick).getImage().getScaledInstance(32, 32, Image.SCALE_SMOOTH)));
                buildingTick.setBounds(200, 55, 32, 32);
                frame.getContentPane().add(buildingTick);
            }
            if(floorTick != null) { //Removes floor dropdown tick
                floorSelected = false;
                frame.getContentPane().remove(floorTick);
            }
            if(roomTick != null) { //Removes room dropdown tick
                roomSelected = false;
                frame.getContentPane().remove(roomTick);
            }

            int i = selectBuilding.getSelectedIndex();
            iCopy = i;
            //Populates floor dropdown menu
            floorNames = new String[allBuilding.get(i).getFloorNum().size()];
            for(int j = 0; j < allBuilding.get(i).getFloorNum().size(); j++) {
                floorNames[j] = allBuilding.get(i).getFloorNum().get(j);
            }
            selectFloor.setModel(new DefaultComboBoxModel(floorNames));
        }
    });


JComboBox selectFloor = new JComboBox();
selectFloor.setBounds(46, 151, 150, 40);
frame.getContentPane().add(selectFloor);
selectFloor.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            floorSelected = true;
            roomSelected = false;
            if(floorSelected == true) {
                BufferedImage tick = null;
                try {
                    tick = ImageIO.read(new File("src/images/tick.png"));
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                floorTick = new JLabel(new ImageIcon(new ImageIcon(tick).getImage().getScaledInstance(32, 32, Image.SCALE_SMOOTH)));
                floorTick.setBounds(200, 146, 32, 32);
                frame.getContentPane().add(floorTick);
            }
            if(roomTick != null) {
                roomSelected = false;
                frame.getContentPane().remove(roomTick);
            }

            int k = selectFloor.getSelectedIndex();
            kCopy = k;
            String floor = floorNames[k];
            roomNames = new String[allBuilding.get(iCopy).getFloorToRomm(floor).size()];
            for(int l = 0; l < allBuilding.get(iCopy).getFloorToRomm(floor).size(); l++) {
                roomNames[l] = allBuilding.get(iCopy).getFloorToRomm(floor).get(l);
            }
            selectRoom.setModel(new DefaultComboBoxModel(roomNames));
        }
    });
Pengibaby
  • 373
  • 2
  • 11
  • 5
    Well, I can see an error in your program, and that's that you're using `setBounds(...)` which suggests you're using `null-layout` which [is evil](https://www.leepoint.net/GUI/layouts/nulllayout.html) as it might produce different outputs when run in different OS / PLAFs / screen sizes and resolutions, just like [this one](https://stackoverflow.com/a/42521097/2180785). Apart from that, we need a proper [mre] that we can copy-paste and run while we do tests for this. – Frakcool Nov 25 '19 at 19:52
  • 2
    `setBounds` suggests you're using `null` layouts, this is going to be problematic (and is highly unrecommended) – MadProgrammer Nov 25 '19 at 21:17

0 Answers0