Java GUI Development – AWT, Swing, and JavaFX


Learn Java GUI development fundamentals, including AWT, Swing, Event Handling, and an introduction to JavaFX for building desktop applications with graphical user interfaces.

In Java, you can develop Graphical User Interfaces (GUIs) using various libraries. The two primary libraries for GUI development are AWT (Abstract Window Toolkit) and Swing, both part of the Java Foundation Classes (JFC). Additionally, JavaFX is a more modern library for building richer, more interactive UIs. Let's dive into the basics and how to use these tools.

1. Basics of AWT (Abstract Window Toolkit)

AWT is Java's first GUI toolkit, introduced in Java 1.0. It provides a set of GUI components such as buttons, labels, text fields, etc., and allows developers to create basic graphical user interfaces.

Key Features of AWT:

  1. AWT components are platform-dependent; they rely on the underlying operating system's windowing system.
  2. AWT uses the peer-based model, where it delegates the rendering of components to the operating system.

Basic AWT Example – Creating a Simple Frame


import java.awt.*;

public class AWTExample {
public static void main(String[] args) {
// Create a frame
Frame frame = new Frame("AWT Example");
// Set the size of the frame
frame.setSize(400, 300);
// Set the layout to null (no layout manager)
frame.setLayout(null);
// Add a label
Label label = new Label("Hello AWT!");
label.setBounds(150, 100, 100, 30); // Set position and size
frame.add(label);
// Show the frame
frame.setVisible(true);
}
}

Key Points:

  1. Frame is the top-level window in AWT.
  2. Label is a simple component to display text.
  3. setLayout(null) allows for manual positioning of components.

2. Swing Components

Swing is a more powerful and flexible GUI toolkit than AWT. It is part of the Java Foundation Classes (JFC) and provides a lightweight, platform-independent framework for building GUIs. Unlike AWT, Swing components do not rely on the operating system's windowing system but are entirely Java-based.

Key Swing Components:

  1. JFrame: A top-level window that contains components like buttons, text fields, etc.
  2. JButton: A button that can trigger actions.
  3. JLabel: A component used to display text or images.
  4. JTextField: A text input field.
  5. JList: A list component.
  6. JComboBox: A drop-down list.

Swing Example – Simple GUI with JButton and JLabel


import javax.swing.*;
import java.awt.event.*;

public class SwingExample {
public static void main(String[] args) {
// Create a JFrame
JFrame frame = new JFrame("Swing Example");
// Set frame size
frame.setSize(400, 300);
// Create a label
JLabel label = new JLabel("Click the button!");
label.setBounds(150, 100, 150, 30);
// Create a button
JButton button = new JButton("Click Me");
button.setBounds(150, 150, 100, 30);
// Add an event listener to the button
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText("Button Clicked!");
}
});
// Add components to the frame
frame.add(label);
frame.add(button);
// Set layout manager to null for manual positioning
frame.setLayout(null);
// Make the frame visible
frame.setVisible(true);
// Set default close operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Key Points:

  1. JFrame is the primary container for Swing applications.
  2. JButton and JLabel are interactive components.
  3. Event handling is achieved by using ActionListener to handle button clicks.

3. Event Handling in Java GUI

Event handling in Java GUI is a mechanism where user actions, such as button clicks, key presses, or mouse movements, trigger events. The event handling system in Java follows the event-driven programming model, where an event (like a button click) triggers an event handler (a method that responds to the event).

Event Handling in Swing:

  1. Listener Interfaces: Interfaces like ActionListener, MouseListener, KeyListener define methods to handle various events.
  2. Event Source: A GUI component (like a button or text field) that generates events.
  3. Event Listener: An object that listens to events and responds when an event occurs.

Example – Handling Button Click with ActionListener:


import javax.swing.*;
import java.awt.event.*;

public class EventHandlingExample {
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("Event Handling Example");
frame.setSize(300, 200);
// Create a button
JButton button = new JButton("Click Me");
// Add ActionListener to the button
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "Button clicked!");
}
});
// Add the button to the frame
frame.add(button);
// Set layout manager to null
frame.setLayout(null);
// Set button position
button.setBounds(100, 50, 100, 30);
// Set frame properties
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Key Points:

  1. ActionListener is used for handling button clicks.
  2. You can add listeners to components using methods like addActionListener().

4. JavaFX Introduction

JavaFX is a modern, rich GUI framework that allows you to build interactive desktop applications. It was introduced as a successor to Swing, providing a more feature-rich, modernized, and flexible approach to GUI development. JavaFX provides support for 2D/3D graphics, animation, media, and web content.

Key Features of JavaFX:

  1. FXML: A declarative XML format for designing the user interface.
  2. Scene Graph: A tree-like structure that holds all UI elements.
  3. CSS Styling: JavaFX supports styling using CSS to enhance the UI.
  4. Event Handling: Similar to Swing, but with a more flexible architecture.

Basic JavaFX Example – Creating a Window with a Button


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 btn = new Button("Click Me");
// Set the button's action
btn.setOnAction(e -> System.out.println("Button clicked!"));
// Set up the layout and add the button
StackPane root = new StackPane();
root.getChildren().add(btn);
// Create a scene
Scene scene = new Scene(root, 300, 250);
// Set the title and scene for the stage (window)
primaryStage.setTitle("JavaFX Example");
primaryStage.setScene(scene);
// Show the stage
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}

Key Points:

  1. Stage: Represents a window in JavaFX.
  2. Scene: A container that holds the layout and UI elements.
  3. Event handling in JavaFX is similar to Swing, but with a more structured model.