JavaFX for Swing Developers
1 The JavaFX Advantage for Swing Developers
JavaFX is designed to provide applications with such sophisticated GUI features as smooth animation, web views, audio and video playback, and styles based on Cascading Style Sheets (CSS).
For more than 10 years, application developers have found Swing to be a highly effective toolkit for building graphical user interfaces (GUIs) and adding interactivity to Java applications. However, some of today's most popular GUI features cannot be easily implemented by using Swing. These features and others described in the following sections can help application developers to meet the full range of modern requirements. Later chapters in this document explain how to use Swing and JavaFX together.
FXML is an XML-based markup language that enables developers to create a user interface (UI) in a JavaFX application separately from implementing the application logic. Swing has never offered a declarative approach to building a user interface. The declarative method for creating a UI is particularly suitable for the scene graph, because the scene graph is more transparent in FXML. Using FXML enables developers to more easily maintain complex user interfaces.
To learn more about the benefits of using FXML, see the Getting Started with FXML document.
JavaFX Scene Builder
To help developers build the layout of their applications, JavaFX provides a design tool called the JavaFX Scene Builder. You drag and drop UI components to a JavaFX Content pane, and the tool generates the FXML code that can be used in an IDE such as NetBeans or Eclipse.
Cascading style sheets contain style definitions that control the look of UI elements. The usage of CSS in JavaFX applications is similar to the usage of CSS in HTML. With CSS, you can easily customize and develop themes for JavaFX controls and scene graph objects.
Using CSS as opposed to setting inline styles enables you to separate the logic of the application from setting its visual appearance. Using CSS also simplifies further maintenance of how your application looks and provides some performance benefits.
JavaFX Media Support
With the media support provided by the JavaFX platform, you can leverage your desktop application by adding media functionality such as playback of audio and video files. Media functionality is available on all platforms where JavaFX is supported. For the list of supported media codecs, see Introduction to JavaFX Media.
For more details, see Leveraging Applications with Media Features chapter.
Animation brings dynamics and a modern look to the interface of your applications. Animating objects in a Swing application is possible but is not straightforward. In the Swing rendering model, painting happens on a double buffer. All alterations of object properties and positions with time are rendered on a double buffer. Only when the painting is completed, is the final result actually painted onto the screen. To show time-based alterations of objects requires significant efforts from a developer using Swing. In contrast, JavaFX enables developers to animate graphical objects in their applications more easily because of the scene graph underlying the platform and the particular APIs that are specifically created for that purpose.
For more details about animation in JavaFX, see Creating Transitions and Timeline Animation in JavaFX. Be sure to check the Tree animation example.
For a long time, Swing developers have wanted the ability to render HTML content in Java applications. JavaFX brought this feature to life by providing a user interface component that has web view and full browsing functionality.
For more details, see Adding HTML Content to JavaFX Applications.