JComboBox is refering to old Frame while removeAllItems()

55 views Asked by At

I tried to figure this out myself but I can't. I'm stuck at a strange problem.

I have a Java Program with multiple classes and forms (I use Intellij and the build in GUI-Creator). When I switch from one Screen to another I just call frame.setVisible(false); at the leafing window and frame.setVisible(true); at the window I want to show next.

On a Button Click I make this:

In Class 1:

if (e.getSource() == umschaltenButton) {
            this.mainW.goToMainWindow();
            logger.log(Level.INFO, "Switched Back to MainMenu");
            frame.setVisible(false);
        }

And here is the weird part.

In Class 2:

public void goToMainWindow() {

            frame = tvElectronics.drawMainWindow(); // I get a new Frame with new Images and so on
            frame.addMouseListener(al);
            frame.add(BotomPanel);  // in here is the JComboBox
            frame.setSize(LENGTH, HEIGHT);

            comboBox1.removeAllItems(); // Here it tryes to refere to the old frame before i made frame = tvElectronics.drawMainWindow();
            Vector<String[]> content = tvElectronics.getContent();

            for (int i = 0; i < tvElectronics.getAnz(); ++i) {
                comboBox1.addItem((i + 1) + ". " + content.get(i)[3]);
            }
            comboBox1.setSelectedIndex(chanel);

            frame.setVisible(true);

        }

And so it tries to update the old frame from class2 which no longer exists because of the new one I just created. And so I have 2 frames open: one as I want it and one strange old frame form class2.

My problem is that I want bind my JComboBox to a new Frame and update it but it is still connected to the old one and that causes weird problems like jumping back in the function. I mean it is at the last line of goToMainWindow() and then it starts again at the first line.

1

There are 1 answers

1
Hovercraft Full Of Eels On
  1. First off you should avoid swapping JFrames as your program does since this is a very annoying GUI design. Please read The Use of Multiple JFrames, Good/Bad Practice? for more on this.
  2. Next, it's impossible for us to tell what GUI view your JComboBox is associated with.
  3. But having said that, it really shouldn't matter. Instead of doing what you're doing, I would give the display class that holds a JCombBox a public method that you call on the containing display class that clears the contained JComboBox's model or that places items in the model. This way, there will be no ambiguity as to which JComboBox you're referring to, and this way you avoid directly exposing a view's inner components.
  4. As an aside, I try to gear my display or view classes towards creating JPanels, not JFrames as this will give my code much greater flexibility.

For example

// my display class
class Display1 {
   private DefaultComboBoxModel<String> myModel = new DefaultComboBoxModel<>();
   private JComboBox<String> myCombo = new JComboBox<>(myModel);

   public void removeAllComboElements() {
      myModel.removeAllElements();
   }

   public void addElement(String ele) {
      myModel.addElement(ele);
   }

}

Same for your Display2 class. Then you can call the correct method on the JComboBox that is held by the correct view/display.

This way, when you swap displays, perhaps by using a CardLayout, you can clear the JComboBox in the display that is being shown by calling its own method to clear its own combobox's model.