Java GUI Development: JavaFX vs Swing

Java GUI Development: JavaFX vs Swing
Java provides two major frameworks for building Graphical User Interfaces (GUI):
1️⃣ Swing – Older, lightweight, and built into Java.
2️⃣ JavaFX – Modern, rich UI toolkit introduced as a replacement for Swing.
1. Swing: The Traditional Java GUI Toolkit
Swing is part of the Java Foundation Classes (JFC) and provides a wide range of GUI components like buttons, text fields, tables, and more.
1.1 Simple Swing Application
import javax.swing.*;
public class SwingExample {
public static void main(String[] args) {
// Create a JFrame (Window)
JFrame frame = new JFrame("Swing Example");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a JButton
JButton button = new JButton("Click Me!");
frame.add(button);
// Show the window
frame.setVisible(true);
}
}
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class JavaFXExample extends Application {
@Override
public void start(Stage primaryStage) {
// Create a Button
Button button = new Button("Click Me!");
// Set up the layout
StackPane root = new StackPane();
root.getChildren().add(button);
// Create the Scene
Scene scene = new Scene(root, 400, 300);
// Set up the Stage (Window)
primaryStage.setTitle("JavaFX Example");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Date: 2025-03-28 00:00:00.000000