• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding Panel Dynamically

 
Siddharth Mitesh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What particular part of the problem are you having difficulties with ?
I'm not going to do your homework for you....
 
Jill Moritz
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic