GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes how to close the previous JWindow Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to close the previous JWindow" Watch "how to close the previous JWindow" New topic
Author

how to close the previous JWindow

Kee Kee moon
Ranch Hand

Joined: Dec 11, 2009
Posts: 147
Users click on a button opening more than one JWindow. User can only close the latest JWindow, but not the previous one.
How to close the previous JWindows?

Thanks
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

maybe it's something to do with JWindow. with JFrame there is no problem. close the second one then use or close the first.


SCJP
Visit my download page
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

How are you even trying to close the JWindows? They don't have a close button by default. They also don't show up in the task bar. That means you must have added something for closing them.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10425
    
    8

Most probably you have one instance reference to the JWindow and everytime a new window is created you are doing something like myJWindow=new JWindow(); Then later on somewhere you are calling myJWindow#setVisible(false) resulting in only the latest one being closed. Of course I am just shooting in the dark here. Like Rob said, you need to tell us more.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Kee Kee moon
Ranch Hand

Joined: Dec 11, 2009
Posts: 147
Maneesh Godbole wrote:Most probably you have one instance reference to the JWindow and everytime a new window is created you are doing something like myJWindow=new JWindow(); Then later on somewhere you are calling myJWindow#setVisible(false) resulting in only the latest one being closed. Of course I am just shooting in the dark here. Like Rob said, you need to tell us more.



I use JWindow to display images. Please see below that is how I open a JWindow. Each time I click on a small image, it
will open an JWindow to enlarge the small image. If I keep click on images without closing (dispose()) the current JWindow,
the program will open more than one JWindow. If I have multiple JWindow are opened, I can only close the latest one I open.
How to close( dispose()) all of JWindow ???


JWindow window = new JWindow() to open a new JWindow.
dispose() to close JWindow.



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

You will need to keep track of the windows. You can use Window.getWindows(), but I'd prefer my own List<JWindow> for this.
Kee Kee moon
Ranch Hand

Joined: Dec 11, 2009
Posts: 147
Rob Spoor wrote:You will need to keep track of the windows. You can use Window.getWindows(), but I'd prefer my own List<JWindow> for this.


Thank you Bob.

This is what I did, please see below and It works. Thank you again Bob.

Window[] windows = window.getWindows();
for (Window win : windows) {
if (!win.getName().startsWith("frame")) { // I don't want to close the JFrame, but JWindow only
win.dispose();
}
}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

It's Rob, not Bob. Also, your code may also close any dialog that is shown. A check against the type (if (window instanceof JWindow) is somewhat safer.
Kee Kee moon
Ranch Hand

Joined: Dec 11, 2009
Posts: 147
Rob Spoor wrote:It's Rob, not Bob. Also, your code may also close any dialog that is shown. A check against the type (if (window instanceof JWindow) is somewhat safer.


I am sorry about your name. I should have changed the font to be bigger, so I can read the web easier.
And, thank for your help again about Dialog window.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to close the previous JWindow