I have a public JFrame
with a CardLayout
, this JFrame
includes some JPanels
for different stages of analysis, I want to return to the main panel and erase all data stored in the objects to start a new analysis using a JMenuItem
, but I don't know which function could do that. Any suggestion?
I have tried with this code, a jMenuItem1ActionPerformed which just backs to that jpanel but doesn't reset the gui. "Seleccion" is the main jpanel, the main menu of the application
panelPrincipal.removeAll();
panelPrincipal.revalidate();
panelPrincipal.repaint();
panelPrincipal.add(seleccion);
panelPrincipal.revalidate();
panelPrincipal.repaint();
There is no "one-size-fits-all" solution, and there is no core Java "function" that you can call for this since it all depends on the structure of your program. In other words, you will have to create your own reset mechanism. Hopefully, your program structure is built around an Model-View-Control (MVC) type of pattern, and if so, then your JMenuItem's listener would notify the Control of the user's wish to reset, the Control would call
reset()
on the Model (a method which you would have to create of course) which would reset the Model to the initial state. The View which should have listeners attached to the Model will then change its display accordingly.