Table of contents
1.
Introduction
2.
What is Java Swing?
3.
Features Of Swing Class  
4.
Java Swing Packages
5.
Components of Swing Class
6.
Differences Between AWT and Swing
7.
Commonly used Methods of the Component class
8.
Hierarchy of Java Swing Classes
9.
Java Swing Examples
9.1.
Example of Swing by Association Inside Constructor
9.2.
Simple Example of Swing by Inheritance
10.
Advantages of Java Swing
11.
Disadvantages of Java Swing
12.
Frequently Asked Questions
12.1.
What are the benefits of using Swing in Java?
12.2.
What is MVC in Java Swing?
12.3.
What are the characteristics of Java Swing?
12.4.
Is the Java Swing platform-dependent?
13.
Conclusion
Last Updated: Jul 25, 2025
Easy

Introduction to Java Swing

Author Juhi Sinha
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Swing is a Java Foundation Classes (JFC) library and a Java Window Toolkit extension (AWT). Swing has many more features than AWT, including new components, expanded component features, and excellent event handling with drag and drop support. Java Swing is a straightforward framework to work with. 

In this blog, we will discuss Java Swing in detail. So, let's get started without any further delay!

What is Java Swing?

The Java Foundation Classes include Java Swing. It is suitable for developing lightweight desktop applications because it is used to create window-based applications.

Java Swing is based on an abstract windowing toolkit API that is completely written in Java.

Java Swing provides lightweight, platform-agnostic components, making it ideal for designing and developing desktop-based applications (systems).

Features Of Swing Class  

The features of the Swing class are as follows:

  • Independent of the platform.
  • Looks and feels pluggable.
  • The architecture is MVC.
  • Components are light in weight.
  • JTable, JTabbedPane, JScollPane, and other advanced features are available.
  • Because Java is a platform-independent language that runs on any client machine, the look and feel of a platform-specific operating system have no bearing on an application's GUI built with Swing components.
  • Lightweight Components: Its AWT supported lightweight component development starting with JDK 1.1. A component must not rely on non-Java system classes to be considered lightweight. The look and feel classes in Java help Swing components have their view.
  • Pluggable Feel and Look: This feature allows users to change the appearance of Swing components without having to restart the application. The Swing library allows components to have the same look and feel across all platforms, regardless of where the programme is running. The Swing library provides an API that allows for complete control over the look and feel of an application's GUI.

Java Swing Packages

  • javax.swing: Contains the core Swing components and containers such as JFrame, JPanel, JButton, and JLabel.
  • javax.swing.event: Provides classes and interfaces for handling events generated by Swing components.
  • javax.swing.border: Contains classes for creating and managing borders around Swing components.
  • javax.swing.table:Provides classes for creating and managing tables, including JTable and TableModel.
  • javax.swing.tree: Includes classes for creating and managing hierarchical data structures, such as JTree.

Components of Swing Class

  • JFrame: Represents a top-level window with standard decorations, such as title bar and border.
  • JPanel: A container used for grouping and organizing other components.
  • JButton: A push button that can trigger actions when clicked.
  • JLabel: Displays a short string or an image icon.
  • JTextField: Allows user input of a single line of text.
  • JTextArea: Allows user input of multiple lines of text.
  • JCheckBox: A check box that can be selected or deselected.
  • JRadioButton: A radio button that is part of a button group where only one button can be selected at a time.
  • JComboBox: A drop-down list that allows users to select one item from a list.
  • JMenuBar: A menu bar that holds menus such as JMenu for organizing menu items.

Also, read about Swing components in java, Why is Java Platform Independent

Differences Between AWT and Swing

There are many differences between Swing and Java Awt that are as follows:

ParametersAWTSwing
LibraryPart of the standard Java library.Part of the Java Foundation Classes (JFC).
ComponentsUses native system components, which can look different on different platforms.Uses lightweight components (pure Java), ensuring a consistent look and feel across platforms.
Look and FeelLimited to the look and feel of the native operating system.Provides pluggable look-and-feel, allowing customization and consistency across different platforms.
ArchitectureBuilt on top of AWT components, extending their functionality.Built entirely in Java, independent of native system components.
Event HandlingUses native event handling mechanisms, which can be less flexible.Provides a more flexible and consistent event handling model through the javax.swing.event package.
PerformanceGenerally faster for native operations due to direct use of native code.May be slower due to its abstraction layer and lightweight components, but offers better portability and consistency.
Component CustomizationLimited customization options due to reliance on native components.Extensive customization options, allowing developers to create complex and highly customized UIs.
Threading ModelUses the native threading model, which can lead to inconsistencies.Uses a single-threaded painting model (Event Dispatch Thread) for consistent UI updates.
Supported ComponentsBasic components like buttons, labels, and text fields.More advanced components like tables, trees, and tabbed panes.

Commonly used Methods of the Component class

The methods of a Component class are widely used in java swing as given below:

MethodDescription
setSize(int width, int height)Sets the size of the component.
getSize()Returns the size of the component as a Dimension object.
setLocation(int x, int y)Sets the location of the component relative to its parent container.
getLocation()Returns the location of the component as a Point object.
setBounds(int x, int y, int width, int height)Sets the size and location of the component.
getBounds()Returns the bounds of the component as a Rectangle object, including its size and location.
setVisible(boolean visible)Sets the visibility of the component. If false, the component is not displayed.
isVisible()Returns true if the component is visible, otherwise false.
setEnabled(boolean enabled)Enables or disables the component. A disabled component is unresponsive to user interaction.
isEnabled()Returns true if the component is enabled, otherwise false.
setForeground(Color color)Sets the foreground color (text or border color) of the component.
setBackground(Color color)Sets the background color of the component.
getForeground()Returns the foreground color of the component.
getBackground()Returns the background color of the component.
addMouseListener(MouseListener l)Adds a mouse listener to the component to handle mouse events.
removeMouseListener(MouseListener l)Removes a mouse listener from the component.
repaint()Requests a redraw of the component.
setFont(Font font)Sets the font of the component.
getFont()Returns the font of the component.

Hierarchy of Java Swing Classes

The Java Swing API hierarchy is shown below:

Java Swing Examples

The frame can be made in two ways:

  • By creating a Frame class object (association).
  • By adding to the Frame class (inheritance).

Example of Swing by Association Inside Constructor

All of the code for creating JFrame, JButton, and method calls can also be written in the java constructor.

import javax.swing.*;  
public class Simple {  
JFrame f;  
Simple(){  
f=new JFrame(); 
         
JButton p = new JButton("click");
p.setBounds(120,100,100, 40);  
         
f.add(p);  
         
f.setSize(300,400);  
f.setLayout(null);
f.setVisible(true); 
}  
 
public static void main(String[] args) {  
new Simple();  
}  
} 

In the example above, setBounds is used to set the button's position.

Simple Example of Swing by Inheritance

We can also inherit the JFrame class, so we don't have to create a JFrame instance explicitly.

import javax.swing.*;  
public class Simple2 extends JFrame{  
JFrame f;  
Simple2(){  
JButton b=new JButton("click");
b.setBounds(100,80,80, 40);  
         
add(b);  
setSize(300,400);  
setLayout(null);  
setVisible(true);  
}  
public static void main(String[] args) {  
new Simple2();  
}} 

Advantages of Java Swing

  • Platform Independence: Provides a consistent look and feel across different platforms since it is written in Java.
  • Rich Set of Components: Offers a wide range of sophisticated and customizable UI components like tables, trees, and tabbed panes.
  • Pluggable Look-and-Feel: Allows developers to change the appearance of applications easily using different look-and-feel themes.
  • Extensibility: Supports customization and extension of existing components to create complex and highly specialized user interfaces.
  • Event Handling: Provides a robust event handling model through the javax.swing.event package, enabling fine-grained control over user interactions.
  • Double Buffering: Reduces flicker and improves rendering performance with built-in double buffering support.

Disadvantages of Java Swing

  • Performance Overhead: Swing components can be slower compared to native components due to their abstraction layer and lightweight nature.
  • Complexity: Can be complex to use and configure, especially for developers unfamiliar with its extensive API and event handling.
  • Memory Usage: Swing applications may consume more memory compared to AWT or native applications due to the overhead of Swing's component hierarchy.
  • Look-and-Feel Consistency: Although Swing provides a consistent look-and-feel, it might not always perfectly mimic the native appearance of components on different platforms.
  • Threading Issues: Requires careful management of the Event Dispatch Thread (EDT) to avoid concurrency issues and ensure thread safety in GUI updates.

Frequently Asked Questions

What are the benefits of using Swing in Java?

Swing extends the functionality of AWT-replacement components by adding new components.

Swing components can change their appearance depending on which "look and feel" library is currently in use.

Swing components adhere to the Model-View-Controller (MVC) paradigm, allowing for a much more flexible user interface.

What is MVC in Java Swing?

Swing's model-view-controller architecture (MVC) is the foundation for all of its components. MVC divides user interface components into three parts. Each of these elements has a significant impact on how the component functions. Each component's state data is included in the model.

What are the characteristics of Java Swing?

  • The architecture is MVC.
  • Components are light in weight.
  • Independent of the platform.
  • JTable, JTabbedPane, JScollPane, and other advanced features are available.

Is the Java Swing platform-dependent?

Java Swing is platform-independent.

Conclusion

Java Swing is a powerful toolkit for building platform-independent, feature-rich desktop applications in Java. With its lightweight components, pluggable look-and-feel, and advanced UI elements, Java Swing offers developers great flexibility and control over GUI design. Despite some performance trade-offs, it remains a reliable choice for creating interactive and cross-platform user interfaces.

Recommended Readings:

Timer In Java Swing

Swing Components in java

Graphics in java swing

Displaying Image in Java Swing

Using ToolTip in Java Swing

 

 

Live masterclass