File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Adding Panel Dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding Panel Dynamically" Watch "Adding Panel Dynamically" New topic

Adding Panel Dynamically

Siddharth Mitesh

Joined: Jan 15, 2004
Posts: 10
I want to add panel at runtime in center of content pane when i press label which is in another west side panel.
Reply fast
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
What particular part of the problem are you having difficulties with ?
I'm not going to do your homework for you....
Jill Moritz

Joined: Sep 16, 2003
Posts: 1
I've done something similar in swapping out 2 different JPanels at runtime, based on some selection the user makes in the window. Both of these JPanels are inherited from the same subclass of JPanel and therefore have the same public methods. By doing this, other code in the window can talk to either JPanel without needing to know which class it is. I use a reference variable of the ancestor JPanel for both.
Here are the variable declarations:
JPanelResult jPanelResult; // descendent of JPanel
JPanelResultSubclass1 jPanelResult1; // descendent of JPanelResult
JPanelResultSubclass2 jPanelResult2; // ditto
// when window is initialized:
jPanelResult = new jPanelResult1; // start out with this panel
jPanelMiddle.add(jPanelResult,...); // add to another content panel
When they get swapped out, the code looks similar to this:
jPanelResult = jPanelResult2; // assign the other one to the var
jPanelMiddle.add(jPanelResult, ...); // add the new one
jPanelResult.revalidate(); // this step and the next one are necessary!
Hope this is helpful -- and not too late.
I agree. Here's the link:
subject: Adding Panel Dynamically
jQuery in Action, 3rd edition