Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to switch panels  RSS feed

 
Barry Brashear
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a container panel which I want to show 1 of 2 possible inner
panels. My start panel is empty I thought I could do this by removing all panels using :

parentPanel.removeAll();

and adding the other panel using

parentPanel.add(panel2);

Afterwards, the empty panel is still showing. Any ideas?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JPanel.html

one of the re...() methods from
"Methods inherited from JComponent"
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another solution is to use a CardLayout - it's exactly made for this purpose.

Moving to our Swing forum...
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
Another solution is to use a CardLayout - it's exactly made for this purpose.

Moving to our Swing forum...


Using GridBagLayout with overlapping components and setting them visible (or not) can also be very useful when you want to swap components around. However, like you said I think CardLayout is exactly what they want.
 
samir Sadiki
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barry,
After adding the new panel, you may have to call on validate() and/or repaint().
 
samir Sadiki
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example to illustrate what I meant.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!