I want to back to the main panel in my java application with a jMenuItem, my panels and other stuff are set with a CardLayout. So I have 3 panels and from any of them I want to be able to return to the first panel using this menu item to start a new analysis. I have tried with the property setVisible with no results. Any suggestion? thanks in advance.
Asked
Active
Viewed 1,338 times
3
-
4Use a controller which is passed to each panel which "knows" how to make the navigation work, [for example](http://stackoverflow.com/questions/24296505/how-to-effectively-use-cardlayout-in-java-in-order-to-switch-from-panel-using-bu/24296872#24296872) or [for example](http://stackoverflow.com/questions/23352226/singleton-with-cardlayout-wont-show-card-when-another-class-calls-singleton-ins/23352348#23352348) – MadProgrammer Jun 22 '15 at 02:26
-
How about removing the panel from the parent container and use the revalidate() method ? – Jalal Sordo Jun 22 '15 at 02:44
-
2@JalalSordo: so you're going to recommend that he forgo use of CardLayout, the preferred way to swap views in Swing and instead jerry rig it by hand? No thank you please. – Hovercraft Full Of Eels Jun 22 '15 at 02:50
-
Yes as long as he has just started putting the ui, and he won't change a lot of code. – Jalal Sordo Jun 22 '15 at 02:57
-
I had a similar project. Its hard to describe the all conditions in few words. However, I made my main panel as singleton. If I call getInstance() it return a new main panel. now I call either home or list view or any other view from the object from getInstance(). – Saqib Rezwan Jun 22 '15 at 03:06
-
For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Jun 22 '15 at 15:40
1 Answers
0
The below example may help you.
import java.awt.CardLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class cardLayout {
JFrame objFrm = new JFrame("CardLayout Demo");
JPanel pnl1, pnl2, pnl3, pnlMain;
JMenuBar mBar;
JMenu mnu;
JMenuItem mnuItem;
public void show() {
pnl1 = new JPanel(new CardLayout());
pnl1.add(new JLabel("Panle 1"));
pnl2 = new JPanel(new CardLayout());
pnl2.add(new JLabel("Panle 2"));
pnl3 = new JPanel(new CardLayout());
pnl3.add(new JLabel("Panle 3"));
pnlMain = new JPanel();
pnlMain.setLayout(new CardLayout());
pnlMain.add(pnl1);
pnlMain.add(pnl2);
pnlMain.add(pnl3);
objFrm.setLayout(new CardLayout());
objFrm.add(pnlMain);
objFrm.setSize(300, 300);
mBar = new JMenuBar();
mnu = new JMenu("Menu");
mnuItem = new JMenuItem("Change Panel");
mnuItem.addActionListener((java.awt.event.ActionEvent evt) -> {
((CardLayout) pnlMain.getLayout()).next(pnlMain);
});
mBar.add(mnu);
mnu.add(mnuItem);
objFrm.setJMenuBar(mBar);
objFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
objFrm.setVisible(true);
}
public static void main(String[] args) {
new cardLayout().show();
}
}
At first i added the three Jpanels(pnl1,pnl2,pnl3) to JFrame having card layout and it didn't worked and throw error. So instead i created one more panel (pnlMain) with cardlayout and added all the three panels. And now it worked fine on menu item click event.

Jeet
- 1,006
- 1
- 14
- 25