How to switch content of two java jpanels
I want to switch the content of jpanels.lets say I have jpanel1 and jpanel2 and I want to change the content of jpanel1 to jpanel2 and also jpanel2 to jpanel1.those panels have completely different content. So I can't change the jpanel properties and all the elements are toggled instead. This image is for clarification
void switchPanels (){
Content c1=jPanel1.getContent();
jPanel1.setContent(jPanel2.getContent());
jPanel2.setContent(c1);
}
I know the above codes don't work. i want to know how i can implement this.
+3
source to share
1 answer
Instead of adding 2 panels and switching their content, I placed them in another JPanel with a BorderLayout (CENTER). To replace them, simply remove the first panel from container 1 and add to container2.
JPanel container1=new JPanel();
container1.setLayout(new BorderLayout());
container1.add(thepanel1);
JPanel container2=new JPanel();
container2.setLayout(new BorderLayout());
container2.add(thepanel2);
public void swap() {
container2.add(thepanel1);
container1.add(thepanel2);
revalidate();
repaint();
}
+3
source to share