1

The structure likes this:

I have a Frame and two button - btnA, btnB

when I press btnA, a createPanel() function will return a panelA to be displayed in the frame,

and so does btnB.

btnA and btnB can be switched.

Before I add the panel into the frame, I use a clearPanel() function to clear the existing panels in the frame.

but the question is when I resize or click the panel, I can see the previous panels that should be removed already.

Is there anything I lost?

public void actionPerformed(ActionEvent e) {
    String buttonString = e.getActionCommand();
    if (buttonString.equals("A")) {
        clearPanel();

        A = new APanel();
        this.getContentPane().add(A.createPanel(), BorderLayout.CENTER);
        this.pack();

        componentMap.put("A", A);
        btnB.setEnabled(true);
        btnA.setEnabled(false);
    }
    else if (buttonString.equals("B")) {
        clearPanel();

        chart = new BPanel();
        this.getContentPane().add(B.createPanel(), BorderLayout.CENTER);
        this.pack();

        componentMap.put("B", B);
        btnA.setEnabled(true);
        btnB.setEnabled(false);
    }
}

private void clearPanel() {
    if (!componentMap.isEmpty()) {  // I store panels in a HashMap
        for (Object o: componentMap.values()) {
            this.getContentPane().remove((JPanel)o);
        }
        this.getContentPane().invalidate();
        componentMap.clear();
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1371541
  • 43
  • 1
  • 8
  • 2
    1) Use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson May 29 '12 at 05:17
  • 1
    That's the reason why I answered on this thread of yours [Why revalidate()/repaint() on JFrame after additions on the run ?](http://stackoverflow.com/questions/10671449/java-swing-questions-in-actionlistener) . `pack()` is not meant for such things, though you went with that answer, so as advised by @AndrewThompson , go with CardLayout if you want to keep the headaches related to `revalidate()/repaint()` away from yourself :-) – nIcE cOw May 29 '12 at 06:17
  • 1
    Have a look at this [example](http://stackoverflow.com/questions/8854939/switching-jpanel-within-main-window/8855076#8855076) and [another example](http://stackoverflow.com/questions/9394137/one-jframe-and-two-jpanels/9394369#9394369) – nIcE cOw May 29 '12 at 06:26

1 Answers1

2

You are adding A.createPanel() and B.createPanel() to the contentPane but you store A and B in your componentMap. Therefore, when you call this.getContentPane().remove((JPanel)o);, you are doing this on A and/or B which are not in the content pane and therefore you don't remove anything.

You could use a simpler/safer approach if you want to clear the content pane:

this.getContentPane().removeAll();
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • Except from the componentMap, I also use this.getContentPane().add() to store A or B. I cannot use removeAll(), because it'll remove some of other controllers on the content pane. – user1371541 May 29 '12 at 13:08
  • @user1371541 post an SSCCE if you want a complete answer. Whatever you are trying to do, the fact that you don't store the correct component that you add to your content pane is definitely a problem (but it's impossible to tell if it is your only problem) – Guillaume Polet May 29 '12 at 13:14