Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Window#toFront() does not activate JFrame

800346Jan 26 2010 — edited Jan 27 2010
Hello,
I noticed that the method "Window#toFront()" is not enough to activate a JFrame that is hidden behind other windows/applications. So I imagined the following hack, but wonder if anyone knows about a better way:
private Robot r;
...
        try {
            r = new Robot();
        } catch (AWTException ex) {
            ex.printStackTrace();
        }
...
private void activateWindow(JFrame frame) {
    frame.setAlwaysOnTop(true);
    frame.setAlwaysOnTop(false);
    Point location = MouseInfo.getPointerInfo().getLocation();
    Point locationOnScreen = frame.getLocationOnScreen();
    r.mouseMove(locationOnScreen.x+100, locationOnScreen.y+10);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);
    r.mouseMove(location.x, location.y);
}
The functionality is needed when clicking an icon in the notification area of the task bar. This should start the swing application only once. When the icon is clicked again, the started application should always activate its main window.

Regards,
André
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 24 2010
Added on Jan 26 2010
12 comments
297 views