Product Development in Brisbane

code snippit to Find the active Window in Swing

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().

Leave a Reply