Is there a way from within a Java application to list all of the currently open/active (I'm not sure the terminology here) JFrames
that are visible on screen? Thanks for your help.

- 8,093
- 8
- 50
- 76

- 1,028
- 2
- 9
- 13
-
3This has the unmistakable odor of bad design. There should generally be only one frame in an app., and if further top-level components are created that are worth worrying about, keep a reference to them. – Andrew Thompson Sep 09 '11 at 16:31
-
You're probably right, but I don't have tremendous experience in programming and I was just trying to test out an alternate idea. Thank you for your comment and answer though. – Connor Neville Sep 09 '11 at 17:05
-
I'll expand on that comment a little. Generally an app. would have 1 frame. Anything else that cannot be included into the frame using any number of clever techniques for cramming multiple components into one GUI (e.g. `CardLayout`, `JSplitPane`, `JTabbedPane`, `JDesktopPane`/`JInternalFrame`..) could be shown in a `JDialog` or `JOptionPane`. The 2nd is modal by default, while the 1st can be specified as modal. This then means that user input to the parent `JFrame` is blocked while the dialog is open. For things like gaining further input, modality is very handy. – Andrew Thompson Sep 09 '11 at 17:18
-
5No that does not have any odor of anything bad. It is a valid design choice. Actually, we start relying on it in JavaX now. – Stefan Reich Oct 12 '15 at 15:32
3 Answers
Frame.getFrames()
returns an array of all frames.
Alternately as mentioned by @mKorbel, Window.getWindows()
will return all windows - since Frame
(& JFrame
) extend Window
that will provide all frames, and then some. It will be necessary to iterate them to discover which ones are currently visible.

- 168,117
- 40
- 217
- 433
I agree with Stefan Reich's comment.
A very useful method is Window.getOwnedWindows()
... and one context where it is useful, if not essential, is TDD (Test-Driven Development): in an (integration) test, where you have various Window
objects on display (JDialog
, etc.), if something goes wrong before the test has finished normally (or even if it finishes normally), you will often want to dispose of subordinate windows in the tests' clean-up code. Something like this (in JUnit):
@After
public void executedAfterEach() throws Exception {
// dispose of dependent Windows...
EventQueue.invokeAndWait( new Runnable(){
@Override
public void run() {
if( app.mainFrame != null ){
for( Window window : app.mainFrame.getOwnedWindows() ){
if( window.isVisible() ){
System.out.println( String.format( "# disposing of %s", window.getClass() ));
window.dispose();
}
}
}
}
});
}

- 132,869
- 46
- 340
- 423

- 14,126
- 11
- 103
- 157
Frame.getFrames()
will do your work.
Returns an array of all Frames created by this application. If called from an applet, the array includes only the Frames accessible by that applet.
A simple example:
//all frames to a array
Frame[] allFrames = Frame.getFrames();
//Iterate through the allFrames array
for(Frame fr : allFrames){
//uncomment the below line to see frames names and properties/atr.
//System.out.println(fr);
//to get specific frame name
String specificFrameName = fr.getClass().getName();
//if found frame that I want I can close or any you want
//GUIS.CheckForCustomer is my specific frame name that I want to close.
if(specificFrameName.equals("GUIS.CheckForCustomer")){
//close the frame
fr.dispose();
}
}
Also you can use Window.getWindows()
as others mentioned.

- 21,001
- 12
- 102
- 104