It's not a secret anymore!
The moose likes Swing / AWT / SWT and the fly likes Swing- dynamic generation of panels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing- dynamic generation of panels" Watch "Swing- dynamic generation of panels" New topic

Swing- dynamic generation of panels

HS Thomas
Ranch Hand

Joined: May 15, 2002
Posts: 3404
I've got a fair knowledge of Swing but I'm stuck
with the following problem.
I've defined a JFrame which contains 3 JPanels
laid out horizontally across and a JScrollPane containing a JTable directly below.
I need to dynamically replace the second JPanel
depending on which Button was clicked on the Navigation Panel.A common scenario.
What is the best way of doing this ? Would it be via some complex event-handling or is it a simple method call.
Could someone please point me in the right direction?
Thanks in advance
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
you could check out teh CardLayout in the java.awt package.
this layout allows you to change panels like in a card deck (thus its names).
if it doesnt solve your need or you dont want to use it, you can just stick to removing and adding diffrent panels all the time as the user presses the button.
Harry Pfohl

Joined: Apr 21, 2001
Posts: 17
You first have to get the button event. When the button is pressed get the SWING-object of your panel. Remove all components inside and then add the new items. Don't forget to initialize the new components first.
If you have to do dynamic panels very often think about building an XML interface (see; run the Example and select the tabbedpane dynamic container). Is is build 100% dynamically because all GUI components are described in an XML file readed at runtime.
[ May 28, 2002: Message edited by: Harry Pfohl ]
[ May 28, 2002: Message edited by: Harry Pfohl ]
I agree. Here's the link:
subject: Swing- dynamic generation of panels
It's not a secret anymore!