Close all open jframes when opening a new one

Asked

Viewed 2,363 times

2

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

I have a login system and when necessary, I invoke another frame to define a password. After setting the password I do the Dispose() to this jframe and start the new jframe, but I would like the login jframe to also close.

Perhaps a more viable solution would be to call a jDialog to define that password, but I don’t feel comfortable when it comes to jDialogs and that’s why I’m invoking a new jframe.

Any suggestions?

EDIT:

I create the 'JFrame Form' as follows:

inserir a descrição da imagem aqui

Then in the design part the name of Frame I don’t know what it is because it only has this:

inserir a descrição da imagem aqui

Just how can I set this frame if I don’t know her name ?

  • That issue of yours still has to do with the same problem?

  • Yes, it was an issue made at @joaoNeto’s request to see if you can help. I haven’t been able to resolve this issue yet

  • That’s exactly what you said in the first post. Then I’ll have to run the frame by parameter. But then I have to put the parameter in the constructor? Plataformalogin(Jframe frame) ?

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);
}

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

You also have another option:

If you are sure which way the user went to get to the frame that is can do so:

structure:

Panel Princiapl --> Painellogin --> Painelalterarpass

Main panel calls login frame: (takes as parameter the reference of the main panel)

login(PainelPrincipal);

In the login constructor you get:

public login(JFrame PainelPrincipal){ ... }

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

alterarPass(PainelPrincipal, PainelLogin);

In the class of alterarPass in the builder you get like this:

  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...

  • But in this code where I refer to the login frame ?

  • This code is to close all frames, not one in specific, for one in specific I will publish now...

  • Friend, this code arrives, because I need to close them all and then open another one. It’s functional :D Thank you again !!!

  • I’m glad it worked, but I posted the two ways I know ;)

  • @Hugomachado just to let you know, check out this answer http://answall.com/a/44265/13237 an interesting solution so you don’t have to jump from frame to frame ;)

0

One suggestion is just set Jframe’s visibility to false.

jframeLogin.setVisible(false);

And when you want it to appear again:

jframeLogin.setVisible(true);
  • But how do I know the name of the login frame? I want to close it or make it invisible by clicking a button of another jframe. When I open the "design" of the login jframe there just says Jframe

  • Somewhere you declared this frame right? There is only you put event of the boot of the other frame the setVisible, in case, you will have to pass the frame as parameter to the other, p that inside the other you can manipulate it, it was a little difficult to understand, later I put an example, now to the cell. Thanks

  • Are you using same code or some framework to create windows? Type windowbuilder?

  • I am only using classes of type 'Jframe Form' ! I when I invoke a new frame open me over or over the login frame. In this new frame by clicking the 'ok' button I want to open a third frame, and close the login frame

  • Edit the question with your code that gets better.

  • But my code will not help you at all, since I do not create frames by code, eg: Jframe frame = new Jframe("frame"); It is simply when creating the class that creates, but I will put in the code my most specified doubt

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.