java
  1. java-swing

Java Swing

Java Swing is a graphical user interface (GUI) toolkit that enables developers to create cross-platform desktop applications. It provides a set of lightweight components that can be customized to create sophisticated GUIs.

Syntax

import javax.swing.*;

public class HelloWorldSwing {
    private static void createAndShowGUI() {
        // Create and set up the window
        JFrame frame = new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Add the label
        JLabel label = new JLabel("Hello World");
        frame.getContentPane().add(label);

        // Display the window
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        // Schedule a job for the event-dispatching thread
        SwingUtilities.invokeLater(() -> createAndShowGUI());
    }
}

Example

import javax.swing.*;

public class MyFrame extends JFrame {
    public MyFrame() {
        // Set the title
        setTitle("My Frame");

        // Set the size
        setSize(300, 200);

        // Set the location
        setLocationRelativeTo(null);

        // Set the defaultCloseOperation
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Add a label
        JLabel label = new JLabel("Hello World");
        getContentPane().add(label);
    }

    public static void main(String[] args) {
        // Create and show the frame
        SwingUtilities.invokeLater(() -> {
            MyFrame frame = new MyFrame();
            frame.setVisible(true);
        });
    }
}

Output

The above code will create a simple frame with a label that displays "Hello World".

Explanation

Java Swing provides a set of lightweight components that can be used to create sophisticated graphical user interfaces (GUIs) for desktop applications. Some of the key components provided by Swing are:

  • JFrame - A top-level container that represents the main window for a desktop application.
  • JLabel - A component that displays a text or image label.
  • JButton - A button that can be clicked to trigger an action.
  • JTextField - A text input field that allows the user to enter text.

Swing components can be arranged using layout managers, which provide a flexible way to position and size components in a container. Some of the layout managers provided by Swing are:

  • BorderLayout - A layout manager that divides a container into five regions: north, south, east, west, and center.
  • FlowLayout - A layout manager that arranges components in a row, wrapping to a new row when it runs out of space.
  • GridLayout - A layout manager that arranges components in a grid of rows and columns.

Use

Java Swing can be used to create cross-platform desktop applications with sophisticated graphical user interfaces (GUIs). Some of the types of applications that can be built using Swing are:

  • Productivity applications - Applications that allow users to create and edit documents, spreadsheets, and other forms of data.
  • Multimedia applications - Applications that allow the playback and manipulation of audio and video content.
  • Games - Applications that allow users to play games.

Important Points

  • Java Swing is a graphical user interface (GUI) toolkit that enables developers to create cross-platform desktop applications.
  • Swing provides a set of lightweight components that can be customized to create sophisticated GUIs.
  • Swing components can be arranged using layout managers, which provide a flexible way to position and size components in a container.

Summary

Java Swing is an essential toolkit for GUI development in Java. Although it has been largely replaced by JavaFX in recent years, it remains a popular choice for developers seeking to create desktop applications with cross-platform support and sophisticated user interfaces. The lightweight nature of Swing components means they can be customized to suit a wide range of design requirements, while layout managers provide a flexible way to arrange them within a container.

Published on: