0

I have a JFrame with a login(JPanel) which can be selected from a menubar. The menubar also shows options depending on users roles. When a user first logs in an instance of the JFrame is created, my problem comes when i select login on the menubar and instanciate en new JFrame since i obviously have 2 JFrames open and i can't find a way to hide or close the previous JFrame from the JPanel. Is there any way to hide/dispose all open windows right before i instanciate a new JFrame or any other possible solution? Thanks in advance for your time!

  • so for example, when login Jframe shows up and everything went right, the login JFrame get hided and main JFrame shows up? – Kick Buttowski Sep 16 '14 at 06:38
  • The login is a JPanel that is opened from a menubar on the main JFrame to change roles. Once you log in using that JPanel, a new JFrame with different options according to the users permissions shows up but the previous JFrame is still there and i can't find a way To hide it from inside the JPanel – Leo Palmieri Sep 16 '14 at 14:17
  • just read the advice that madprogrammer told you. he is the best, and if you still have issue lets us know – Kick Buttowski Sep 16 '14 at 14:20
  • I'll try with the JDialog as madprogrammer said. I didn't think of using a JDialog but that's a new approach worth trying. Thanks for the advice! I'll let you know if i still couldn't make it work – Leo Palmieri Sep 16 '14 at 14:51
  • for sure. if he says, the sky is not blue you should accept it cuz he is that great :) – Kick Buttowski Sep 16 '14 at 14:52

1 Answers1

2

Start by taking a look at The Use of Multiple JFrames, Good/Bad Practice?.

Instead of using a second JFrame, using a JDialog of some kind to show the login window. A modal dialog will block the caller when the dialog is made visible, allowing the code to continue once it's closed. This is very helpful for gain information from the user...

For example, use this to gather the credentials (and possibly authenticate) the user and allow the caller to extract the results when the dialog is closed.

Have a look at How to Make Dialogs for more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I remember there were other thing that you could open windows inside the main windows? I cannot remember that? – Kick Buttowski Sep 16 '14 at 06:40
  • Using a JDialog where i can input username and password and then pass them to another class sounds like a good idea. I guess that way i can do a Myframe.this.setvisible(false); and a JFrame Myframe = new JFrame(); I'll check it out to see if it works! – Leo Palmieri Sep 16 '14 at 14:29
  • I checked the code and my login window is a JDialog already, not a JPanel as i said before. Sorry for the mistake! It is modal and it's in a separate class from the main JFrame, that's why i can't find a way to close that JFrame when I make a new JFrame once valid username and password are entered – Leo Palmieri Sep 16 '14 at 17:20
  • Your work flow is wrong. The frame should open the dialog. Once the dialog is closed, the frame should collect the information it needs from the dialog and change states. Avoid using multiple frames, it just confuses people – MadProgrammer Sep 16 '14 at 20:34