Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing Panels  RSS feed

 
Donovan Brock
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Is there any way for changing the panel that is currently there for a new one?


Thanks
 
Chris Corbyn
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. add() and remove() can both be called repeatedly. In fact, with something like GridBagLayout you can mask one component with another by placing it in the same position on the grid (albeit at a complete waste of memory and crappy code )

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any time you replace a component you need to (for swing) revalidate the parent
so that it lays out its components again

parentComponent.remove(thePanel);
parentComponent.add(newPanel);//depends on layoutmanager as to how you add
parentComponent.revalidate();
parentComponent.repaint();//sometimes required, sometimes not - safer to include

if you have a number of panels you want to switch between, perhaps using
a CardLayout() might suit you better - then it's just show the panel you want
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!