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 graphical user interface (GUI) for Java programs. Java Swing is also known as 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 complex tree and table. Besides emulating look and feel of various platforms, Swing also provides the pluggable look and feel to allow look and feel of Java programs independent from the underlying platform.

Swing Architecture

Swing is platform independent and enhanced MVC (Model –View – Controller) framework for Java application. 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 some top-level containers.  Lightweight means component renders or paints itself using drawing primitives of the Graphics object instead of relying on the host operating system (OS). As the 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 of its components 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 the picture below.  UI delegate is responsible for painting screen 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:

PackagesDescription
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 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 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.

 

  • Was this tutorial helpful ?
  • YesNo