2

I have a middlePanel that displays panel1 with 6 button in it. On clicking a button, other components are displayed in mainPanel and finally returning back to panel1.

The problem I face here is : the components aren't shown at once - I got go move cursor around to make it visible. The code I use to set the panels is :

public void SetMainPanel(JPanel panel) {
    middlePanel.removeAll();
    this.middlePanel = panel;
    panel.setVisible(true);
    this.middlePanel.setVisible(true);
    mainPanel.add(middlePanel, BorderLayout.CENTER);
    middlePanel.revalidate();
    middlePanel.repaint();
    mainPanel.revalidate();
    mainPanel.repaint();
}

See am set visible to the passed panel, and middlePanel in which it is added. updateui & validate middlePanel and mainPanel that contains middlePanel. Yet why is this problem.

Can anyone help me know why is this problem and how o solve it. I feel their is a way to get rid of this problem but couldn't recall it.

Tvd
  • 4,463
  • 18
  • 79
  • 125
  • Many calls to `validate()`, `updateUI()`? Why don't just use `repaint()`? – ecle Nov 18 '11 at 15:35
  • @eee, It's also necessary to have the layout manager layout it's components again, which `repaint()` doesn't do. – mre Nov 18 '11 at 15:39
  • 1
    do you call this method in the Event dispatch thread? – josefx Nov 19 '11 at 11:58
  • @josefx, I call this method from other Panel class and pass its instance to this method to set it as the middlePanel. I call it normally : myParent.SetMainPanel(new DataPanel(this)); – Tvd Nov 19 '11 at 13:43

2 Answers2

5

From an answer I gave someone else when asked about dynamically adding/removing components:

When dynamically adding/removing components from a container, it's necessary to invoke revalidate()/validate() and repaint() afterward. The former will force the container to layout its components again and the latter will remove any visual "artifacts".

And by the way, in this scenario, it's completely unnecessary to invoke updateUI(). And as a suggestion, perhaps using a different layout manager (e.g. CardLayout) would serve you better.

Community
  • 1
  • 1
mre
  • 43,520
  • 33
  • 120
  • 170
  • 1
    +1, for revalidate and for noting updateUI is completely unnecessary. Also, whenever I see the removeAll() method I would also recommend the CardLayout. – camickr Nov 18 '11 at 15:51
  • @mre, eee, dk89, as you experts suggested, I commented updateUI() and added mainPanel.repaint() in the last. That didn't solved so even added middlePanel.repaint() after middlePanel.validate(). But that also didn't solve. The output is the same - got to move mouse around. – Tvd Nov 19 '11 at 09:01
  • REG layout, the main reason am using Border (mainPanel) & middlePanel is Free layout is I want to resize components on change of screen size. The panel that am passing to middlePanel is GridLayout. CardLayout wont give me that facility. Any suggestions for that. – Tvd Nov 19 '11 at 09:06
  • Tried revalidate() instead of validate(), but no success. Though the process was a bit faster but had to move cursor around. – Tvd Nov 19 '11 at 09:10
  • @mre, camickr, I have 5 panels at this moment to replace on clicks - later that will be increased. Do you think adding so many panel instances to CardLayout is a good idea. Wont it unnecesarrily take memory and slow down the process. – Tvd Nov 19 '11 at 11:43
  • 1
    *"Do you think adding so many panel instances to CardLayout is a good idea."* 5? **LOL!** Try adding 5000 and see if it slows the JRE down. Report back. – Andrew Thompson Nov 19 '11 at 13:52
  • 1
    I tried implementing CardLayout & it works very well. No repaint/refresh problem. Thanks to all. – Tvd Nov 23 '11 at 07:19
2

Try calling mainPanel.repaint()

Dimitry
  • 4,503
  • 6
  • 26
  • 40