Introduction to Java Swing
Swing is a Java Foundation Classes (JFC) library and an extension of the Abstract Window Toolkit
(AWT). Java Swing offers much-improved functionality over AWT, new components, expanded
components features, and excellent event handling with drag and drop support.
Swing is a set of API (API - set of classes and interfaces).
Swing is provided to Design Graphical user interfaces.
Swing is an Extension library to the AWT (Abstract Window Toolkit).
Includes New and improved components that have been enhancing the looks and functionality of
GUIs.
Swing can be used to build the standalone Swing GUI Apps as Servlets and Applets.
It employs model view design architecture.
Swing is more portable and more flexible than AWT. The Swing is built on top of the AWT.
Swing is Entirely written in Java.
Java Swing Components are platform independent and the Swing components are lightweight.
Swing supports a pluggable look and feel and also provides more powerful components such as
tables, lists, scroll panes, color chooser, tabbed pane etc.
Further Swing follows MVC.
Java AWT Java Swing
1. Java AWT is an API to develop GUI application in Java.
1. Swing is a part of Java Foundation Classes and is used to creat
2. Components of AWT are heavy weighted. 2. The Components of Java Swing are light weight.
3. Components are platform dependent. 3. Components are platform independent.
4. Execution Time is more than Swing. 4. Execution Time is less than AWT.
5. AWT Components require [Link] package. 5. Swing components require [Link] package.
MVC Architecture
The model-view-controller (MVC) architectural pattern is employed by swing components. This
pattern separates the application into three interconnected components. Model -> Represents the
data and business logic.
View -> Displays the data from the model to the user.
Controller -> Handles user input and updates the model and view accordingly.
Basic Swing Components
Text Field (JTextField): Allow users to input single-line text.
Buttons (JButton): Trigger action when clicked.
Toggle Button: A button with two states: selected and unselected.
JCheckbox: A component allowing the user to select or deselect an option.
Radio Button: Typically used in groups where only one option can be selected at a time within that
group.
import [Link].*;
import -> The statement is used to make classes from other packages accessible within the current
source file.
[Link] -> Core package that provides a set of classes for building GUIs.
* -> Wild card character indicates all public classes within the package should be imported.
Applets and Application
Applets:
- Designed to be embedded within web pages and run in a web browser.
- While historically used, applets are largely considered obsolete due to security concerns and rise
of web technologies.
Applications:
- Standalone Java programs that run directly on a user's OS, providing robust user experience.
- Swing is primarily used for developing these standalone desktop applications.
Pluggable Look and Feel
Swing pluggable look and feel allows developers to change the visual appearance of an application
at run-time without altering the underlying code. This means a Swing application can be made to
look like Windows, MacOS or Linux.