0

I am building a desktop student management app in java. So I need to close the current JFRAME while an another JFRAME is visible(after closing the new jframe old jframe should be visible ). Could you please help me ?

Nipuna C
  • 81
  • 2
  • 7

2 Answers2

1

Your basic idea is generally not recommended. You can have a look at The Use of Multiple JFrames: Good or Bad Practice? for the long winded discussion.

Generally it's recommended to use a CardLayout for these type of operations, see How to use CardLayout for more details

What you want to try and do is decouple of the navigation decision making from the views themselves - there's no reason why the first and second view should ever care about each other OR be backing decisions about how the user should navigate.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You can use JFrame#dispose to close just one frame, this ofcourse requires a reference to the JFrame you want to dispose

Dinh
  • 759
  • 5
  • 16
  • But I need to show the old JFrame as soon as I close the new Jframe. – Nipuna C Oct 05 '17 at 08:18
  • Do they need to be two seperate Windows? If not take a look at CardLayout https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html – Dinh Oct 05 '17 at 08:20
  • Yes the should be in separate windows . – Nipuna C Oct 05 '17 at 08:20
  • Play arround with dispose on both Frames, to make one visible again you have to call pack or show according to this article: https://stackoverflow.com/questions/2037132/java-swing-dispose-a-jframe-does-not-clear-its-controls – Dinh Oct 05 '17 at 08:23
  • Thanks RSDinh for your support but it didn't solve my problem :( – Nipuna C Oct 05 '17 at 08:25