Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Switching JPanels  RSS feed

 
Daniel Searson
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my program a certain JPanel changes as the user changes between JDialogs.
To remove the panel, I am calling remove() within the JPanel containing it, giving a reference to the JPanel that is being removed. This seems to work fine.
Then, to add the new JPanel, I call add() and pass in a reference to the new JPanel and its position (BorderLayout.CENTER).
The problem is that this new panel does not show until I resize the overall JFrame containing it. This strikes me as a very strange problem. Is there a method I need to call when changing JPanels in this manner?
Thanks,
Daniel
[This message has been edited by Daniel Searson (edited March 06, 2001).]
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daniel,
You need to add following two lines to your code after u add new JPanel to it.

here fr is the container to which you are adding /removing panels

hope this helps.
regards
deeksha


 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't a card layout be better for what you are trying to do?
 
Daniel Searson
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks deeksha! The code you gave worked perfectly.
Bodie: For what I doing, the BorderLayout is working fine. The dialogs that are being switched are actually editable and the panel needs to change to reflect the dialog being edited...
Daniel
 
tmtully
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am did the same exact thing as the original posting, but when the new panel goes out of focus, it doesn't get repainted when it comes back in. For example, if I minimize it and bring it back, it's gone. How do I solve that? Thanks.
 
Al Heinrichs
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try doing a validate() and then a repaint(). That's been working in my code.
 
amol bakre
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Use CardLayout it is much faster and easier and everything that it should be and u can make visible different panels by calling the methods provided by cardlayout from a menu or a button!!
amol
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!