Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java Swing Tutorial: Building Robust GUI Applications

What is Swing in Java?

Java Swing is a sophisticated, platform‑independent GUI toolkit that extends the Abstract Window Toolkit (AWT). It offers a rich set of widgets—buttons, tables, trees, and more—allowing developers to craft polished desktop applications without reinventing basic components.

As part of the Java Foundation Classes (JFC), Swing builds on AWT’s core functionality while providing lightweight, customizable components. The result is a framework that supports advanced UI features, such as look‑and‑feel changes, custom painting, and data binding.

In this tutorial, you’ll master:

Java Swing Class Hierarchy Diagram

Java Swing Tutorial: Building Robust GUI Applications

All Swing components inherit from JComponent and can be added to container classes.

What is a Container Class?

Container classes are the backbone of any Swing UI—they hold and organize other components. At a minimum, a Swing application requires one container. There are three primary container types:

  1. JPanel – A lightweight, non‑window container used for grouping components.
  2. JFrame – A top‑level window that includes a title bar, borders, and system menu.
  3. JDialog – A modal or modeless pop‑up window for user prompts.

What is GUI in Java?

A Graphical User Interface (GUI) in Java delivers an intuitive visual layer for users to interact with applications. By assembling buttons, labels, text fields, and other widgets, developers create engaging experiences that abstract underlying logic.

How to Build a GUI in Java (Step‑by‑Step)

Below is a practical walk‑through that illustrates how to construct a basic window and progressively add components.

Step 1 – Create a New Java File

Copy the following code into your editor:

import javax.swing.*;
class GuiDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("My First GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
JButton button = new JButton("Press");
frame.getContentPane().add(button);
frame.setVisible(true);
}
}

Step 2 – Compile and Run

Save the file, compile with javac GuiDemo.java, then execute with java GuiDemo. A 300 × 300 window containing a single button should appear.

Step 3 – Add Another Button

Modify the code to include a second button:

import javax.swing.*;
class GuiDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("My First GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
frame.getContentPane().add(button1);
frame.getContentPane().add(button2);
frame.setVisible(true);
}
}

Step 4 – Observe the Layout Issue

Running the code now shows two overlapping buttons because the default layout manager for JFrame is BorderLayout, which places components in five distinct regions. Without specifying a region, subsequent components replace the previous ones.

Java Layout Managers

Layout managers control how components are arranged within containers. Choosing the right manager is critical for responsive, maintainable GUIs. The most frequently used managers are:

BorderLayout

BorderLayout divides a container into five areas: NORTH, SOUTH, EAST, WEST, and CENTER. It is the default manager for JFrame.

Java Swing Tutorial: Building Robust GUI Applications

FlowLayout

FlowLayout arranges components sequentially in a row, wrapping to the next line as needed. It is the default manager for JPanel.

Java Swing Tutorial: Building Robust GUI Applications

GridBagLayout

GridBagLayout offers the most flexibility, allowing components to occupy multiple rows or columns and to align precisely within a grid.

Java Swing Tutorial: Building Robust GUI Applications

Example: Building a Simple Chat Window

Below is a complete program that demonstrates how to combine menus, a text area, and a control panel into a cohesive chat interface.

Java Swing Tutorial: Building Robust GUI Applications

Try coding it yourself before reviewing the solution.

// A complete Swing application that includes menus, a text area, and controls
import javax.swing.*;
import java.awt.*;
class GuiDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Chat Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
// Menu bar
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("FILE");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(helpMenu);
JMenuItem openItem = new JMenuItem("Open");
JMenuItem saveItem = new JMenuItem("Save as");
fileMenu.add(openItem);
fileMenu.add(saveItem);
// Bottom panel with FlowLayout (default)
JPanel panel = new JPanel();
panel.add(new JLabel("Enter Text"));
panel.add(new JTextField(10));
panel.add(new JButton("Send"));
panel.add(new JButton("Reset"));
// Text area in the center
JTextArea textArea = new JTextArea();
// Add components to frame using BorderLayout
frame.getContentPane().add(BorderLayout.SOUTH, panel);
frame.getContentPane().add(BorderLayout.NORTH, menuBar);
frame.getContentPane().add(BorderLayout.CENTER, textArea);
frame.setVisible(true);
}
}

Java

  1. Build a Reliable Timer in VHDL: Counting Clock Cycles to Hours
  2. Java Arrays Tutorial: Declaration, Creation, and Initialization – Example Code
  3. Creating an Array of Objects in Java: A Step‑by‑Step Guide
  4. Mastering Java Packages: Creation, Importing, and Best Practices
  5. Creating Custom Exceptions in Java: A Step‑by‑Step Guide
  6. Generate Random Numbers in Java: Practical Guide with Random and Math.random
  7. Java Swing Tutorial: Building Robust GUI Applications
  8. Java 10: Enhancing Performance with Class-Data Sharing (CDS)
  9. Develop a PLCnext Console Application Using C#: A Beginner’s Guide
  10. Optimizing Java 9 Apps on PLCnext Control Using Jlink