Introduction to Java Swing

Java Swing is a part of Java Foundation Classes (JFC) which was designed for enabling large-scale enterprise development of Java applications.

Java Swing is a set of APIs that provides the graphical user interface (GUI) for Java programs. Java Swing is also known as the Java GUI widget toolkit.

Java Swing or Swing was developed based on earlier APIs called Abstract Windows Toolkit (AWT). Swing provides richer and more sophisticated GUI components than AWT.

The GUI components are ranging from a simple label to a complex tree and table. Besides emulating the look and feel of various platforms, Swing also provides a pluggable look and feel to allow the look and feel of Java programs independent from 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 in Swing architecture.

  • Pluggable look and feel:  Swing supports several looks and feels and currently supports Windows, UNIX, Motif, and native Java metal look and feel. Swing allows users to switch look and feel at runtime without restarting the application. By doing this, users can make their own choice to choose which look and feel is the best for them instantly.
  • Lightweight components: All swing components are lightweight except for some top-level containers.  Lightweight means the component renders or paints itself using drawing primitives of the Graphics object instead of relying on the host operating system (OS). As a result, the application presentation is rendered faster and consumed less memory than previous Java GUI applications.
  • Simplified MVC: Swing uses simplified model-view-architecture (MVC) as the core design behind each component called model-delegate. Based on this architecture, each swing component contains a model and a UI delegate. A UI delegate wraps a view and a controller in MVC architecture, as in the picture below.  UI delegate is responsible for painting screens and handling GUI events. Model is in charge of maintaining information or states of the component.
Java Swing MVC - Model Delegate
Java Swing MVC – Model Delegate

Swing packages

Here is the short description of each package in Java Swing:

javax.accessibilitySupports assistive technologies which help people with disabilities adapt to the 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.colorchooserProvides 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.filechooserContains 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 a wide range of classes and interfaces to support a pluggable look and feel.
javax.swing.tableContains classes to support data manipulation in table or spreadsheet format.
javax.swing.textContains classes and interfaces that are supporting 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.treeContains classes and interfaces to define models and views for a hierarchical tree component.
javax.swing.undoContains classes and interfaces supporting undo-able functionality.