Week 35 — What are some differences between JavaFX and Swing?
Question of the Week #35
What are some differences between JavaFX and Swing?
6 Replies
Swing is light,
JavaFX applications have better ui,
JavaFX is more complex,
JavaFX apps take up more space
----------------------------------------
An example Swing program:
An example JavaFX program:
Submission from 5kygalaxy#0000
JavaFX and Swing differ in the following ways:
Design and architecture: Swing uses the MVC Pattern, while JavaFX uses the Scene Graph Architecture.
Appearance and behaviour: Swing adapts to the native operating system, while JavaFX has its own look and feel.
Graphics and animation: JavaFX supports hardware-accelerated graphics and animation, while Swing uses the Java 2D API.
Layout managers: Swing provides several layout managers, while JavaFX provides more powerful layout panels.
CSS styling: JavaFX allows styling with CSS, while Swing does not natively support this.
Web integration: JavaFX provides better integration with web technologies, while Swing does not provide native web integration.
JavaFX is deprecated in Java 11 and is being developed further by OpenJFX, an open source implementation.
Submission from stormofgalaxy#0000
I'm stupid but if I recall JavaFX uses XML for specifying the layout and also has better media support, and other shit. However it's not supported everywhere.
Submission from PNJ3.0#1519
JavaFX and Swing are both Java GUI frameworks.
Swing is part of the JDK since Java 1.2 while JavaFX was only present in OracleJDK installations of Java 7 up to Java 10 and not in OpenJDK JDKs (some OpenJDK vendors still provide Java installations including JavaFX).
JavaFX is now maintained in a separate OpenJDK project called OpenJFX. When using JavaFX, one has to use external libraries.
JavaFX applications have a main class extending
Application
. JavaFX then provides a Stage
which represents a GUI window where the developer can put UI elements in.
In Swing, a central class is JFrame
which represents a window in the UI (similar to Stage
in JavaFX).
JavaFX comes with a module called javafx.fxml which allows to create FXML (a dialect of XML) files that specify what the GUI should look like, what listeners are bound etc. These FXML files are typically created/edited in a program called "Scene Builder". They can then be loaded from a JavaFX application and JavaFX will automatically construct the GUI.
It is possible to create "controllers" which interact with the loaded GUI by registering listeners in the FXML file.
Swing does not support Window builders (a drag-and-drop way of putting GUIs together) by itself but there are Window builders like the Eclipse WindowBuilder™ that generate Swing code.
⭐ Submission from dan1st#0000