JavaFX: Getting the Frame of a Stage

The JavaFX Stage class, version 1.2 has some simple methods for dealing with focus, toBack() and toFront().  What it is lacking is a way of setting the “always on top” property, to force the window to the front.

A quick search revealed several hacky things, but when I read Rakesh Menon’s Always On Top post to the end, I found the tidbit I needed:  java.awt.Frame.getFrames().  It’s a static method that lists all AWT frames in use by the system.  So, for as long as JavaFX uses AWT/Swing (not too much longer, apparently), we can use that.  Here’s the code to get your Stage as a Frame, all above-board.  What you do with it is up to you.

function getFrame(frameName:String):java.awt.Frame {
    var frames = java.awt.Frame.getFrames();
    for(frame in frames) {
        if (frameName == frame.getTitle()) {
            return frame
        }
    }
    return null;
}
This entry was posted in java and tagged . Bookmark the permalink.

Leave a Reply

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