File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Closing all open windows and opening new one Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Closing all open windows and opening new one" Watch "Closing all open windows and opening new one" New topic
Author

Closing all open windows and opening new one

Sham Phadtale
Ranch Hand

Joined: Jul 29, 2008
Posts: 75
Hello,
I have swing apllication in which after password reset I want to take user to Login screen. For that I want to close all open windows and open Login window so that user can login with new credentials. Is there any class/method in java which can be used to close all windows?
System.exit() terminates whole application which i do not want.

Thanks in advance!

Sham
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495


Good Luck,
John Price aka cc11rocks


“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Just use a modal JDialog or JOptionPane#showXxxDialog for your login form.


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

If you really need to close all windows, then java.awt.Window has a static method to retrieve all windows for the current JVM.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

A word of caution: the array returned by that static method includes references to windows that are no longer visible or even displayable -- even windows that are no longer in scope. On all current Sun (Oracle?) JVMs a call to System.gc() removes those, but that behavior isn't mandated by the JLS nor the JVM spec. Code calling this method without first collecting garbage should preferably declare the array in a very limited scope so as not to hold on to references to windows that are no longer in use, thus preventing their collection. Alternatively, while looping over the array, any reference to a non-visible window could be set to null.
Sham Phadtale
Ranch Hand

Joined: Jul 29, 2008
Posts: 75
I think container.dispose() will dispose the frame from which it got call. In my application there might be no of windows are opened.
After resetting password from one window it should close all previous windows and open new window - LOGIN window. So user can process with new credentials as old credentials are not valid now. It is similar to change password og google or yahoo where user goes to login page after resetting his password.
Sham Phadtale
Ranch Hand

Joined: Jul 29, 2008
Posts: 75
Is it right way to use java.awt.Window AWT package in Swing code?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Only the static methods. If you need to create instances you should use JWindow instead.
Sham Phadtale
Ranch Hand

Joined: Jul 29, 2008
Posts: 75
Thanks allot all of you!

Now I am able to close all previous windows and open new Login window.

java.awt.Window win[] = java.awt.Window.getWindows();
for(int i=0;i<win.length;i++){
win[i].dispose();
}
new LoginWindow();>
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Did you even read the word of caution I posted?
Sham Phadtale
Ranch Hand

Joined: Jul 29, 2008
Posts: 75
I am sorry, i did not read it. Now just read, do you mean I should call garbage collector before calling getWindows() method?
Sham Phadtale
Ranch Hand

Joined: Jul 29, 2008
Posts: 75


Is it right?>
Sham Phadtale
Ranch Hand

Joined: Jul 29, 2008
Posts: 75
Hey Darryl, the code written in my last post is it what you want to say in your word of caution? Please correct me if I am going wrong.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Yes, that's right, but next time please UseCodeTags <-- link. I've added them for you this time.

I would just explicitly set the array variable to null after the loop (or let it fall out of scope) but there's no harm in setting individual elements to null if the array remains in scope a bit longer. Not that it helps in any way to have an empty array lying around.

One way of ensuring that the array isn't still in scope after the loop isThe variable window is local to the loop, and no copy is made of the array.

In case you're not familiar with that loop construct, read about it here.
Sham Phadtale
Ranch Hand

Joined: Jul 29, 2008
Posts: 75
Thanks allot Darryl!!
I will definitely take care of UseCodeTags from next time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Closing all open windows and opening new one
 
Similar Threads
How to display info to user after login for some time?
Closing popup windows that have timed out
Clear firefox cookies on closing the window
Issue in session manegement
Can I close windows opened with "target=_blank"??