Closing all possible GUI related elements
Is there a way to close开发者_StackOverflow社区 (or get a handle to) all possible windows, dialogs, prompts, etc. created with Java?
I have an app that creates GUI elements here and there (while it automatically runs tests and such) and sometimes it leaves these elements open. I would like to be able to close all those things from one place.
Edit. I am teh retarded xP
http://download.oracle.com/javase/6/docs/api/java/awt/Window.html#getWindows%28%29
This will give you every Window
(parentclass of Frame
, Dialog
, JWindow
, Grandparent of JFrame
, JDialog
, etc).
Original
Well this gets all of the Frame
s that are open. I don't think there is an equivalent for Dialog
, but there is the getOwnedWindows()
method on Window
. I don't know who owns dialogs constructed with a null
Frame
argument.
Set a collection and then make it close each element.
EDIT:
Ok, here is an exemple, I don't know if ArrayList is the best choice here :
class Main{
public static final ArrayList<Window> containers = new ArrayList<Window>();
public static final void closeEverything(){
ListIterator<Window> list = containers.getListIterator();
while(list.hasNext()){
list.next().dispose();
}
}
}
Then, when you create a Window that you wish to be disposable, you just have to add :
Main.containers.add(this);
in the constructor.
精彩评论