JavaFX 8u20 Days of Future Past (Always On Top)
It’s been a long time since I’ve posted topics relating to JavaFX. So, if you are still following along, awesome!
Introduction
In this blog post I want to blog about a very cool feature starting with JavaFX 8 update 20 that allows your application to always be on top of other applications. What this means is that on your desktop your JavaFX based application can be a floating widget that will remain above all other applications (z-order). An example would be a weather widget in the upper right corner never to be obscured by other applications. So, I’m very excited to share with you this amazing feature ‘Always On Top’.
History
In the past Java Swing developers would rely on the method Window.setAlwaysOnTop(boolean). This feature allowed Swing developers to build native looking and native behaving desktop applications. Ever since JavaFX 1.x this very feature was highly requested (originally requested by Stephen Chin @steveonjava for the WidgetFX framework). This feature request is JIRA ticket RT-153. Figure 1 is the feature request shown with a status of ‘Resolved’.
Although this feature didn’t get into JavaFX versions 1.x, 2.x and the version prior to Java 8u20, it is finally here now. For those who don’t know how to report bugs or file new features requests I encourage you to head over to the JavaFX JIRA system.
Example: Weather Widget
Assuming you know the basics of JavaFX since version 2.0 a typical application would consist of extending from the javafx.application.Application class. When developing JavaFX desktop applications the platform API would provided you with a (javafx.stage.Stage) window. The Stage object will have the following methods to access the always on top property.
- alwaysOnTopProperty()
- setAlwaysOnTop(boolean)
- isAlwaysOnTop()
The following code snippet sets the Stage to be always on top using the method setAlwaysOnTop(boolean).
public class KeyholeDemo extends Application { @Override public void start(Stage primaryStage) { primaryStage.initStyle(StageStyle.TRANSPARENT); primaryStage.setAlwaysOnTop(true); // code omitted... } public static void main(String[] args) { launch(args); } }
How it works
Shown in the listing above the start() method sets the stage to be transparent to be a JavaFX window without a title bar. This allows applications to have irregular shaped windows on the desktop. If a web person trolls you again about JavaFX ask them the following question: “Can HTML5 create irregular shaped windows on the desktop? (always on top)”. Next, the stage is setup to be always on top via the setAlwaysOnTop() method.
Demo
After realizing that this highly requested feature was available I basically updated my old ‘KeyholeWidget‘ project at the Github and created a video demonstrating the widget on my desktop. Here is the video:
To see the full code listing visit the Github project ‘KeyholeWidget‘.
I hope you’ve enjoyed this cool feature (I know I did). As usual please leave comments below.
Happy coding!
Reference: | JavaFX 8u20 Days of Future Past (Always On Top) from our JCG partner Carl Dea at the Carl’s FX Blog blog. |