code snippit to Find the active Window in Swing

Published by Rob on October 27th, 2004 - in Java

To find the active Window(be it a frame or a dialog) in a java swing application you can use the following recursive method:

Window getSelectedWindow(Window[] windows) {

    Window result = null;

    for (int i = 0; i < windows.length; i++) {

        Window window = windows[i];

        if (window.isActive()) {

            result = window;

        } else {

            Window[] ownedWindows = window.getOwnedWindows();

            if (ownedWindows != null) {

                result = getSelectedWindow(ownedWindows);

            }

        }

    }

    return result;

}

For the first call, pass in all the frames for the application with Frame.getFrames().

Related posts:

  1. Looking for TDD Swing code
  2. Writing Test Driven Swing ñ my plan
  3. Technique for getting JUnit tests to compile
  4. JavaScript hacking

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© Rob@Rojotek