Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Closing all open windows and opening new one

 
Sham Phadtale
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Good Luck,
John Price aka cc11rocks
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just use a modal JDialog or JOptionPane#showXxxDialog for your login form.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really need to close all windows, then java.awt.Window has a static method to retrieve all windows for the current JVM.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it right way to use java.awt.Window AWT package in Swing code?
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only the static methods. If you need to create instances you should use JWindow instead.
 
Sham Phadtale
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you even read the word of caution I posted?
 
Sham Phadtale
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Is it right?>
 
Sham Phadtale
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks allot Darryl!!
I will definitely take care of UseCodeTags from next time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic