This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding Panel Dynamically" Watch "Adding Panel Dynamically" New topic
Author

Adding Panel Dynamically

Siddharth Mitesh
Greenhorn

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
thanx
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
Greenhorn

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:
jPanelMiddle.remove(jPanelResult);
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!
jPanelResult.repaint();
Hope this is helpful -- and not too late.
 
wood burning stoves
 
subject: Adding Panel Dynamically
 
Similar Threads
JPopupMenu on top
invisible canvas
jbutton
how to inherit actionEvent?
JPanel question ...