Swing is a part of the Java Foundation Classes (JFC) and has been around for a long time. It is a lightweight, platform-independent GUI toolkit for Java. Swing components are written entirely in Java, which means they have a consistent look and feel across different operating systems. Swing provides a wide range of components such as buttons, labels, text fields, and tables, which can be used to build complex GUIs.
JavaFX is a more modern GUI toolkit for Java. It was introduced to address some of the limitations of Swing and provide a more rich and interactive user experience. JavaFX uses a scene graph architecture, which allows for more efficient rendering and better support for animations and multimedia. It also has a built - in support for CSS - like styling, making it easier to create visually appealing GUIs.
import javax.swing.*;
import java.awt.*;
public class SwingExample {
public static void main(String[] args) {
// Create a JFrame (window)
JFrame frame = new JFrame("Swing Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
// Create a JLabel
JLabel label = new JLabel("Hello, Swing!");
label.setHorizontalAlignment(JLabel.CENTER);
// Add the label to the frame
frame.getContentPane().add(label, BorderLayout.CENTER);
// Make the frame visible
frame.setVisible(true);
}
}
In this example, we first create a JFrame
which represents the main window of our application. We set its title, close operation, and size. Then we create a JLabel
with some text and set its alignment. Finally, we add the label to the frame and make the frame visible.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class JavaFXExample extends Application {
@Override
public void start(Stage primaryStage) {
// Create a label
Label label = new Label("Hello, JavaFX!");
// Create a layout
StackPane root = new StackPane();
root.getChildren().add(label);
// Create a scene
Scene scene = new Scene(root, 300, 200);
// Set the scene to the stage
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX Example");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
In this JavaFX example, we extend the Application
class and override the start
method. We create a Label
, a StackPane
layout, and a Scene
. Then we set the scene to the Stage
(the main window in JavaFX) and show it.
BorderLayout
, FlowLayout
, GridLayout
, and GridBagLayout
. For example, to use a GridLayout
in Swing:import javax.swing.*;
import java.awt.*;
public class SwingGridLayoutExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing GridLayout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(2, 2));
frame.add(new JButton("Button 1"));
frame.add(new JButton("Button 2"));
frame.add(new JButton("Button 3"));
frame.add(new JButton("Button 4"));
frame.pack();
frame.setVisible(true);
}
}
VBox
, HBox
, GridPane
, and StackPane
. Here is an example of using a GridPane
in JavaFX:import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class JavaFXGridPaneExample extends Application {
@Override
public void start(Stage primaryStage) {
GridPane gridPane = new GridPane();
Button button1 = new Button("Button 1");
Button button2 = new Button("Button 2");
Button button3 = new Button("Button 3");
Button button4 = new Button("Button 4");
gridPane.add(button1, 0, 0);
gridPane.add(button2, 1, 0);
gridPane.add(button3, 0, 1);
gridPane.add(button4, 1, 1);
Scene scene = new Scene(gridPane, 300, 200);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX GridPane Example");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SwingEventHandlingExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing Event Handling");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click me");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "Button clicked!");
}
});
frame.getContentPane().add(button);
frame.pack();
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 JavaFXEventHandlingExample extends Application {
@Override
public void start(Stage primaryStage) {
Button button = new Button("Click me");
button.setOnAction(e -> {
System.out.println("Button clicked!");
});
StackPane root = new StackPane();
root.getChildren().add(button);
Scene scene = new Scene(root, 300, 200);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX Event Handling");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Both Swing and JavaFX are powerful GUI toolkits for Java, but they have different characteristics. Swing is a mature and well - established toolkit that is suitable for simple and legacy applications. JavaFX, on the other hand, is a more modern and feature - rich toolkit that is better suited for creating rich and interactive user interfaces. When choosing between the two, developers should consider the project requirements, development resources, and the target user experience.