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
Eric McIntyre
Eric McIntyre16mo ago
Swing is light, JavaFX applications have better ui, JavaFX is more complex, JavaFX apps take up more space ---------------------------------------- An example Swing program:
import javax.swing.*;
import java.awt.*;

public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel buttonPane = new JPanel();
buttonPane.setBackground(Color.RED);

JButton button = new JButton("hello");

buttonPane.add(button);


frame.add(buttonPane);
frame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;

public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel buttonPane = new JPanel();
buttonPane.setBackground(Color.RED);

JButton button = new JButton("hello");

buttonPane.add(button);


frame.add(buttonPane);
frame.setVisible(true);
}
}
Eric McIntyre
Eric McIntyre16mo ago
An example JavaFX program:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Hello extends Application {

@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(Hello.class.getResource("hello-view.fxml"));

Scene scene = new Scene(fxmlLoader.load(), 320, 240);
scene.getRoot().setStyle("-fx-background-color: #FF0000;");

stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
launch();
}
}
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Hello extends Application {

@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(Hello.class.getResource("hello-view.fxml"));

Scene scene = new Scene(fxmlLoader.load(), 320, 240);
scene.getRoot().setStyle("-fx-background-color: #FF0000;");

stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
launch();
}
}
Submission from 5kygalaxy#0000
Eric McIntyre
Eric McIntyre16mo ago
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
Eric McIntyre
Eric McIntyre16mo ago
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
Eric McIntyre
Eric McIntyre16mo ago
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.
public class MyJavaFXApplication extends Application{
public static void main(String[] args){
Application.launch(MyJavaFXApplication.class, args);
}
@Override
public void start(Stage primaryStage){
Text text = new Text();
text.setText("Hello World");
VBox box = new VBox();//container with elements stacked vertically
box.getChildren().add(text);
Scene scene = new Scene(box );//a scene is what goes on in the stage - here, we create a Scene with a 'Text' element
primaryStage.setScene(scene);//bind the scene to the stage
primaryStage.show();//show the stage/main window
}
}
public class MyJavaFXApplication extends Application{
public static void main(String[] args){
Application.launch(MyJavaFXApplication.class, args);
}
@Override
public void start(Stage primaryStage){
Text text = new Text();
text.setText("Hello World");
VBox box = new VBox();//container with elements stacked vertically
box.getChildren().add(text);
Scene scene = new Scene(box );//a scene is what goes on in the stage - here, we create a Scene with a 'Text' element
primaryStage.setScene(scene);//bind the scene to the stage
primaryStage.show();//show the stage/main window
}
}
In Swing, a central class is JFrame which represents a window in the UI (similar to Stage in JavaFX).
public class MySwingApplication{
public static void main(String[] args){
JFrame jframe = new JFrame();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel();
label.setText("Hello World");
jframe.getContentPane().add(label);
jframe.pack();
jframe.setVisible(true);
}
}
public class MySwingApplication{
public static void main(String[] args){
JFrame jframe = new JFrame();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel();
label.setText("Hello World");
jframe.getContentPane().add(label);
jframe.pack();
jframe.setVisible(true);
}
}
Eric McIntyre
Eric McIntyre16mo ago
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.
public class MyJavaFXApplication extends Application{
public static void main(String[] args){
Application.launch(MyJavaFXApplication.class, args);
}
@Override
public void start(Stage primaryStage){
FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("resources/fxml/MyUI.fxml"));//create an FXMLLoader for loading the FXML file located under resources/fxml/MyUI.fxml on the classpath
AnchorPaneroot = loader.load();//load the FXML file - this returns the root element, for example an AnchorPane
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}
public class MyJavaFXApplication extends Application{
public static void main(String[] args){
Application.launch(MyJavaFXApplication.class, args);
}
@Override
public void start(Stage primaryStage){
FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("resources/fxml/MyUI.fxml"));//create an FXMLLoader for loading the FXML file located under resources/fxml/MyUI.fxml on the classpath
AnchorPaneroot = loader.load();//load the FXML file - this returns the root element, for example an AnchorPane
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}
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
Want results from more Discord servers?
Add your server