#Week 35 — What are some differences between JavaFX and Swing?

7 messages · Page 1 of 1 (latest)

hoary lintelBOT
#
Question of the Week #35

What are some differences between JavaFX and Swing?

nimble ledgeBOT
#

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);
    }
}
#

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();
    }
}
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.

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);
  }
}