Home > Articles > Web Development

  • Print
  • + Share This
This chapter is from the book

3.1 What Is JavaFX?

If the NetBeans Platform is written with Swing for its GUI and I already know Swing, why should I learn and use JavaFX? This is a good question. Here are several reasons why.

  • JavaFX provides a rich graphical user interface. While you can certainly provide rich content with Swing, JavaFX has the structure and APIs specifically for animation, 2D and 3D geometry, charts, special effects, color gradients, graphical controls, and easy manipulation of media, including audio, video, and images. The bottom line: you can create rich content a whole lot easier using JavaFX than with Swing and Java2D.
  • JavaFX graphics rendering takes advantage of hardware-accelerated capabilities. This makes rendering graphics and animation perform well.
  • You can embed JavaFX content within Swing panels with JFXPanel. The magic of this specialized Swing component lets you create sophisticated NetBeans Platform applications and include JavaFX rich content in your application windows. Furthermore, you can start using JavaFX without throwing away existing Swing applications.

A Bit of History

JavaFX began as a declarative scripting language (JavaFX Script) that was built on top of Java. While developers enjoyed the ease of a declarative script, it was difficult to integrate JavaFX Script with existing Swing applications. Furthermore, JavaFX Script required learning a new language.

JavaFX 2.0 was released in 2011 and is based on Java APIs. With Java 7, JavaFX is included in the standard release, and beginning with Java 8, which includes 3D capabilities with JavaFX 8, all JavaFX libraries (JAR files) are included in the standard classpath. This means that JavaFX is now part of the Java standard when you download the Java Development Kit (JDK).

While you can certainly use JavaFX APIs in the traditional Java coding style, JavaFX also provides FXML, a declarative XML markup language that describes the graphical components in your application. Scene Builder is a stand-alone application that generates FXML markup. With Scene Builder, you drag-and-drop controls and shapes to design the UI in a visual editor. These coding options make it easier to construct complicated UIs and work with user experience designers. You can also style your JavaFX application with CSS, a standard that is used and known by many designers. We’ll introduce FXML in this chapter and further explore FXML and Scene Builder in the next chapter.

The Scene Graph Metaphor

JavaFX programs with a graphical user interface define a stage and a scene within that stage. The stage represents the top-level container for all JavaFX objects; that is, the content area for the application’s window frame. The central metaphor in JavaFX for specifying graphics and user interface controls is a scene graph. A scene defines a hierarchical node structure that contains all of the scene’s elements. Nodes are graphical objects, such as geometric shapes (Circle, Rectangle, Text), UI controls (Button, TreeView, TextField, ImageView), layout panes (StackPane, AnchorPane), and 3D objects. Nodes can be containers (parent nodes) that in turn hold more nodes, letting you group nodes together. The scene graph is a strict hierarchical structure: you cannot add the same node instance to the graph more than once. Figure 3.1 shows the hierarchical structure of a JavaFX scene graph.

Figure 3.1

Figure 3.1 The JavaFX stage and scene

Parent nodes are nodes that contain other nodes, called children. A child node with no children nodes is a leaf node. With parent nodes, you can include Panes (layout containers) and Controls (buttons, table views, tree views, text fields, and so forth). You add a node to a parent with



            myParent.getChildren().addAll(childNode1, childNode2);

The power of the JavaFX scene graph is that, not only do you define the visual aspect of your application in a hierarchical structure, but you can manipulate the scene by modifying node properties. And, if you manipulate a node property over time, you achieve animation. For example, moving a node means changing that node’s translateX and translateY properties over time (and translateZ if you’re working in 3D). Or, fading a node means changing that node’s opacity property over time. JavaFX properties are similar to the JavaBean properties we’ve already presented, but JavaFX properties are much more powerful. Applying these transformations to a node generally propagates to any of the node’s children as well.

Single-Threaded Model

Like Swing, JavaFX uses a single-threaded model. The JavaFX scene graph must be manipulated in the JavaFX Application Thread, a separate thread from Swing’s Event Dispatch Thread.1 Like Swing, working with a single-threaded UI is mostly transparent: events are sent and received on the JavaFX Application Thread. And like Swing, threading issues arise when you create tasks to execute on a background thread. The solutions to these common programming scenarios are similar.

How do JavaFX and Swing code co-exist? Will you be writing intertwined graphical spaghetti code? No, not at all. In this chapter you’ll learn about JavaFX without any Swing. Then when you learn how to integrate JavaFX into a NetBeans Platform application window, you’ll see how to keep the JavaFX code separate, cohesive, and highly modular. Both the NetBeans Platform architecture and JavaFX program structure make it easy to add and maintain JavaFX rich content.

  • + Share This
  • 🔖 Save To Your Account