Skip to main content

Differences between JavaFX and Swing

NOTE : Oracle hasn't completely abandoned Swing — it isn't deprecated, and Swing applications continue to work. But no work is being done anymore to enhance Swing, and Oracle has made it clear that JavaFX is the future.

Here are ten basic differences between JavaFX and Swing.

In JavaFX, all the world's a stage

In Swing, the class that holds your user interface components is called a frame and is defined by the JFrame class. A frame is essentially an empty window to which you can add a panel, which serves as a container for your user-interface elements. A panel is defined by the JPanel class.
A Swing application is actually a class that extends the JFrame class. To display user-interface components, you add components to a JPanel and then add the panel to the frame.
JavaFX uses the metaphor of a theater to model the top-level containers of an application. A stage (defined by the Stage class) represents the highest level container — typically, a window, but on some operating systems, the stage may represent the entire display surface. The individual controls and other components that make up the user interface are contained in a scene (defined by the Scene class). An application can have more than one scene, but only one of the scenes can be displayed on the stage at any given time.
A scene contains a scene graph, which is the most important concept in JavaFX. The scene graph is a collection of all the elements that make up a user interface — groups, layouts, controls, and shapes. These objects are called nodes and are all derived from the Node class. The Node class has many useful features and capabilities that are automatically made available to any object you can add to your user interface. For example, the Node class defines a setRotate method that allows you to rotate any node in the scene graph.

In JavaFX, layout managers are nodes

In Swing, a layout is associated with a JPanel. The top-level JPanel gives the basic layout for the frame. If you want to nest additional layouts within the top-level layout, you must create additional JPanels, set their associated layouts, and then add them to the higher-level layout.
In JavaFX, layouts are subclasses of the Node class just like controls and shapes. Layouts contain a collection of nodes, and any node contained in a layout can be any kind of node: a control, a shape, or another layout. This scheme is much less cumbersome than Swing's association of layouts with panels.

JavaFX has improved event handling

Both JavaFX and Swing use event handling to respond to user input events. However, you will find that events in JavaFX are better thought-out and more consistent than their equivalents in Swing. But the biggest reason event handling is improved in JavaFX is because of its reliance on properties.

JavaFX supports properties

JavaFX supports the concepts of properties and uses properties extensively throughout its classes. Simply put, a property is a variable whose value can be observed. You can register a listener with any property, allowing you to write code that's triggered automatically whenever the property changes. In addition, you can bind properties to each other, meaning that if one property value changes, the other property value automatically changes with it.
Because nearly all characteristics of user-interface elements are managed through properties, JavaFX provides event handling that's unheard of in Swing. For example, in JavaFX, you can attach an event listener to the color of a shape. If the shape changes color, your event listener code is executed. You may not have a use for the capability, but you can use it if you need to.

JavaFX is skinnable with CSS

One of the best features of JavaFX is that you can control formatting with Cascading Style Sheets (CSS). Just about every aspect of the appearance of your user interface can be set by a style rule, and you can easily allow the user to select which of several available style sheets to apply to the scene. Thus, you can change the entire appearance of your application with a single method call.

JavaFX has more consistent controls

In general, you'll find that the JavaFX control hierarchy is more complete than Swing's. Both have all the basic controls — buttons, check boxes, combo boxes, and the like. But JavaFX has several interesting controls that Swing doesn't have, such as the collapsible TitledPane control and the Accordion control that arranges multiple TitledPane controls in a stack. And the fact that all these controls are skinnable by CSS gives them a huge advantage over Swing.

JavaFX has special effects

The javafx.scene.effect package contains a number of classes that can easily apply special effects to any node in the scene graph. These classes let you easily apply shadows, reflections, blurs, and other interesting visual effects that can transform the appearance of your user interface.

Animation is easier in JavaFX

Animation is possible in Swing, but Swing does not provide any direct support for it. To animate a Swing component, you must set up your own timers and build your own logic to perform the animation.
In contrast, JavaFX has built-in support for sophisticated animations that can be applied to any node in the scene graph. You can use one of several built-in transition classes to perform common animations such as fades, rotations, or motion paths. Or, you can use the KeyFrame and Timeline classes to easily set up custom animations.

JavaFX supports modern touch devices

Swing lacks any support for modern touch devices. In contrast, JavaFX has built-in support for common touch gestures such as scrolling, swiping, rotating, and zooming. Handling these events in JavaFX is as easy as handling any other type of event: You simply install an event listener on the touch event and then write code that responds appropriately.

JavaFX has no equivalent to JOptionPane

It wouldn't be fair to completely fill this list of ten differences with points that clearly favor JavaFX. Here's one minor annoyance of JavaFX: It has no built-in equivalent to Swing's JOptionpane class, which is very handy for displaying short alert messages or getting simple input from the user. In JavaFX, you have to craft your own equivalent to this useful class.

JavaFX got their own Dialog after JDK 8u8*

 As oracle said JavaFX is future, So they are working hard to improve all the lacking points of JavaFX. Here's the new Alert dialog has been introduced in a latest release of JDK 8 update 80 or above.

Comments

Popular posts from this blog

Java 21 Features With Example

Java 10 and 10 Big Java Milestones