Close all open jframes when opening a new one

2

How can I close all open jframes in my project when I execute an action that opens another jframe?

I have a login system and when needed, I invoke another frame to set a password. After setting the password I make the dispose () to this jframe and start the new jframe, but I would like the login jframe to also close.

Maybe a more viable solution would be to call a jDialog to set this password, but I do not feel comfortable with jDialogs and that's why I'm invoking a new jframe.

Any suggestions?

EDIT:

I create ' JFrame Form ' as follows:

TheninthedesignpartthenameoftheFrameIdonotknowwhichisbecauseitonlyhasthis:

So how can I set this frame if I do not know her name?

    
asked by anonymous 09.12.2014 / 16:09

2 answers

1

I think this is the code you need:

System.gc();  
for (Window window : Window.getWindows()} {  
    window.dispose();  
    // por vezes pode ser melhor usar setVisivel(false);
}

Attention: This code closes all frames. If any of your frames has the setting of setDefaultCloseOperation as EXIT_ON_CLOSE the program will terminate, then you should change to HIDE_ON_CLOSE

You also have another option:

If you are sure of what path the user followed to get to the frame you are able to do:

structure:

PanelPrinciapl - > LOGIN Dashboard - > Panel ChangePass

main panel calls the login frame: (takes the reference from the main panel as a parameter)

login(PainelPrincipal);

In the login builder you receive:

public login(JFrame PainelPrincipal){ ... }

If you need to change the password, in the login it calls the frame change password that takes as parameter the PainelPrincipal and the PainelLogin :

alterarPass(PainelPrincipal, PainelLogin);

In the alterarPass class in the constructor you get:

  public alterarPass(Jframe PainelPrincipal, Jframe PainelLogin){
      ...
 }
 //depois fazes o dispose destes dois frames onde precisares
  PainelPrincipal.dispose();
  PainelLogin.dispose();

But imagine that he dutrante this path opened another frame, this will remain open, that is why I say that the first approach is better ...

    
12.12.2014 / 12:58
0

One suggestion is to just set the JFrame's visibility to false.

jframeLogin.setVisible(false);

And when you want it to appear again:

jframeLogin.setVisible(true);
    
10.12.2014 / 02:09