Introduction to Java Swing

Java Swing is a part of Java Foundation Classes (JFC), designed to enable large-scale enterprise development of Java applications.

Java Swing consists of APIs that provide the graphical user interface (GUI) for Java programs. It is also known as the Java GUI widget toolkit.

Java Swing, or simply Swing, was developed based on earlier APIs called Abstract Windows Toolkit (AWT). Swing offers richer and more sophisticated GUI components compared to AWT.

The GUI components range from simple labels to complex trees and tables. In addition to emulating the look and feel of various platforms, Swing also provides a pluggable look and feel to allow Java programs’ appearance to be independent of the underlying platform.

Swing Architecture

Swing is a platform-independent and enhanced MVC (Model –View – Controller) framework for Java applications.

Here are the most important features of Swing architecture:

  • Pluggable Look and Feel: Swing supports several look and feel options, including Windows, UNIX, Motif, and the native Java Metal look and feel. Users can switch between these looks and feel at runtime without restarting the application. This allows users to choose the look instantly and feel that best suits their preferences.
  • Lightweight Components: All Swing components are lightweight, except for some top-level containers. Lightweight means that the component renders or paints itself using drawing primitives from the Graphics object instead of relying on the host operating system (OS). As a result, Swing applications render faster and consume less memory than previous Java GUI applications.
  • Simplified MVC: Swing uses a simplified Model-View-Controller (MVC) architecture as the core design behind each model-delegate component. In this architecture, each Swing component contains a model and a UI delegate. The UI delegate wraps a view and a controller in the MVC architecture, as illustrated in the picture below. The UI delegate is responsible for painting screens and handling GUI events, while the model maintains information or states of the component.
Java Swing MVC - Model Delegate
Java Swing MVC – Model Delegate

Swing packages

Here’s the description of each package in Java Swing:

PackagesDescription
javax.accessibilitySupports assistive technologies that help people with disabilities adapt to Java applications, including audible text reader and screen magnification.
javax.swingContains core swing components, including most of the model interfaces and helper classes.
javax.swing.borderContains classes for different kinds of borders wrapping around components.
javax.swing.colorchooserProvide the color dialog chooser to allow users to choose a wide range of colors using a visual interface.
javax.swing.eventDefines listeners and events, which swing components use to communicate asynchronously between objects of classes.
javax.swing.filechooserContain classes to support file chooser dialog to allow the user to choose files and directories.
javax.swing.plafPLAF stands for the pluggable look and feel. It contains many classes and interfaces to support a pluggable look and feel.
javax.swing.tableContain classes to support data manipulation in table or spreadsheet format.
javax.swing.textContain classes and interfaces that support a well-known design known as document/view.
javax.swing.text.htmlSupports reading and formatting HTML text through the editor.
javax.swing.text.html.parserContains classes and interfaces for HTML parsing.
javax.swing.text.rtfSupports reading and formatting Rich Text Format (RTF) text through the editor.
javax.swing.treeOffer classes and interfaces for dealing with
javax.swing.undoProvide classes and interfaces supporting undo-able functionality.