1

I am facing a issue with implementing back button functionality.

I have 2 frames (Main Frame and Second Frame), so when i press a button on Main frame it takes me to second frame. On the second frame i have a back button it takes me back to the main frame. This is working as expected. But the problem is that once i am back on the main frame after pressing the back button all the alignment of the main frame goes away.

On Main frame i have below code to goto second frame:

frame.dispose();
frame.setVisible(false);
WebAppTest object = new WebAppTest();
object.createAndShowGUIWebAppTest();

On the second frame i am using below code to go back to main frame:

MainLanding object = new MainLanding();
object.createAndShowGUIMainLanding();
frame1.dispose();
frame1.setVisible(false);
  • 1
    I would recommend you to create a [Minimal, Complete, Tested and Readable example](http://stackoverflow.com/help/mcve) so it is a lot easier for us to help you. – Christian Tapia Jan 17 '14 at 22:31
  • The example in [Implementing back/forward buttons in Swing](http://stackoverflow.com/questions/5654926/implementing-back-forward-buttons-in-swing) uses `CardPanel` for this. – trashgod Jan 17 '14 at 23:08

1 Answers1

1

You would be better off using something like a single JFrame containing a CardLayout and selecting the correct panel that you need within the CardLayout rather than switching between completely different frames. So you have one window and then multiple different panels within that window and you choose the one to display at any given time.

If you do still decide to go down the separate-frames route you shouldn't dispose the first frame as well as hiding it. Just hide it and then reveal it again when you want to go back.

Tim B
  • 40,716
  • 16
  • 83
  • 128
  • 2
    See also [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) (I think this answer identified the best alternate strategy.) – Andrew Thompson Jan 17 '14 at 23:26