• Post Reply Bookmark Topic Watch Topic
  • New Topic

change panel added to a layoutManager...

 
Carlo Trani
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can I change panel added to a layoutManager.
Here is the code
<tt>
JPanel edit = new JPanel(); <br>
CardLayout card = new CardLayout();<br>
JPanel panel = new JPanel();<br>
panel.setLayout(card);<br>
panel.add(edit,"INSERT_MODE");<br>

edit JPanel contains other GUI stuff...
when a particular event occur I want to change the edit panel with anoter one...
...I know how to manage the event handling but I am not able to change that layout.
Any suggestion?
TIA
Carlo
 
Shashi Kanta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The following code is an extension to your code to show an example:
JPanel edit = new JPanel();
JPanel secondPanel = new JPanel();
JPanel blankPanel = new JPanel(); //this is going to be the blank panel. do not draw anything in it,
but create the size same as the edit panel and second panel
CardLayout card = new CardLayout();
JPanel panel = new JPanel();
panel.setLayout(card);
panel.add(edit,"INSERT_MODE");
panel.add(secondPanel,"SECOND");
panel.add(blankPanel,"BLANK");
panel.card.show(panel,"INSERT_MODE"); //default panel to be shown first time or whatever u want

Then, during the event handling, suppose you want to show secondPanel, then just show the blankPanel first and then
the secondPanel. This is to avoid repainting problems you may face.
panel.card.show(panel,"BLANK");
panel.card.show(panel,"SECOND");
Hope you get what what you want!
Shashi
 
Carlo Trani
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot Shashi but maybe I did not explain in the right way my problem.
Just to continue the reference of the previous code...
I simply want sobstitute edit panel whit a new version of "that" edit JPanel.
I know how to use show method or even others (first(), last(), next() to flib between previously added panels) but I need something different!
 
Shashi Kanta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You mean to say you want to change the contents of Edit Panel?!
If, so before removing existing components or adding new components call the method invalidate()
then after everything has been modified call the method validate(). in some case you may need to call repaint() also after validate().
But, I suggest you use a new panel and use the help of the card layout to do the switching. If you are not using the card layout, then I don't understand why you have created it!! I got misleaded!!
regards,
Shashi
 
Carlo Trani
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shashi! :-)
You understood perfectly, really thanks a lot for your last post! I find it really useful! You touched the problem
Bye Carlo
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!