Java Swing Tutorial

The goal of this tutorial is to help software professionals learn Java programming in a straightforward manner. After completing this tutorial, you will have a solid understanding of Java swing concepts and be at an intermediate level of expertise, from which you can advance to higher levels.

Swing is an addition to the Abstract Window Toolkit (AWT) and a library of Java Foundation Classes (JFC). Compared to AWT, Swing provides significantly better functionality, new components, expanded component features, excellent event handling, and drag and drop support.

It is the main GUI toolkit for the Java programming language. It is a component of the JFC (Java Foundation Classes), an API for giving Java programmes a graphical user interface. Java was used to write the entire thing.

Java Swing Tutorial - Table Of Contents:

What is Java Swing?

Swing is a component of the standard Java distribution and has roughly four times as many User Interface [UI] components as AWT. By today’s application GUI requirements, AWT is a limited implementation, not really capable of providing the components needed to create complex GUI’s required in modern commercial applications. In comparison to equivalent Swing resources, the AWT component set has a lot of bugs and uses a lot of system resources.

Salient Features of Java Swing:

As we already know that Java swing is the main toolkit for language, let us now understand the features of java swing that sets it apart from other languages. The most prominent and important ones are:

Lightweight: Because Swing API controls are primarily rendered using pure JAVA code rather than underlying operating system calls, Swing components are lightweight and independent of native operating systems' APIs.
Rich controls : Swing offers a wide range of sophisticated controls, including controls for tables, sliders, tabbed panes, and trees.
Highly Customizable : Because visual appearance is independent of internal representation, Swing controls can be customized very easily.
SWING-based GUI applications with pluggable look-and-feel allow for run-time look and feel changes based on the values that are available.

Want to become a Java Developer?  Visit here to learn "Core Java Online Training"

Java Swing Basics and Components:

1. Container class:

A container class is any type of class that contains other components. It is required to have at least one container class when developing GUI applications. The three different container classes are as follows:
Panel: A panel is used to arrange parts on a window.
Frame: A fully functional window with titles and icons is called a frame.
Dialog: Comparable to a pop-up window but less functional than the frame

2. Jbutton class:

It is used to make a button that is labeled. When the button is pressed, an action will be taken using the ActionListener. The class it inherits is AbstractButton, and it is cross-platform.

 MindMajix YouTube Channel

3. JtextField class:

It allows single line text editing and is an inheritor of the JTextComponent class. A single line of text can be edited using JTextField, a small component. Where it is reasonable to do so, JTextField is designed to be source-compatible with java. awt. TextField.

4. Jscrollbar class:

It is used to add a horizontal and vertical scroll. Creates a scrollbar with the orientation, value, extent, minimum, and maximum specified. JScrollBar(int orientation, int value, int extent, int min, int max) The "extent" refers to the area that can be seen. The "visible amount" is another name for it.

5. Jpanel class:

It provides room for an application to which any other component may be attached and derives from the JComponent class. The Java Swing package contains JPanel, a container for storing a collection of components. JPanel's primary function is to organize components. It allows for a variety of layouts that improve component organization, but it lacks a title bar.

6. Jlabel:

JLabels are object components used to add text to containers. One line of read-only text is displayed on it. An input event is not reacted to by a label. It is unable to focus the keyboard as a result.

7. Jcolorchooser:

A JColorChooser offers a control panel made to let the user adjust and choose a color. It offers a set of controls that are intended to let a user choose and manipulate colors.

8. JcheckBox:

A JCheckBox is a graphical (GUI) element that has two possible states: on (true) and off (false). A checkbox is made using the JCheckBox class. It is used to toggle an option between true and false (false). A CheckBox's state can be changed from "on" to "off" or from "off" to "on" by clicking on it. It comes from the class JToggleButton.

9. JRadioButton:

A graphical (GUI) element with a on (true) or off (false) state is the JRadioButton class. in the team

10. JComboBox:

An element of a JComboBox presents a show-up menu of options to the user. A component of the Java Swing package is JComboBox. JComboBox is a JComponent class inheritor. JComboBox displays a popup menu with a list, from which the user can choose an option. JComboBox can be read-only or editable depending on the programmer's preference.

Also, Check out: "Java Frameworks List"

11. JTextField:

One line of text can be edited using a text component called a JTextField object. A single line of text can be edited using JTextField, a small component. Where it is reasonable to do so, JTextField is designed to be source-compatible with java. awt. TextField.

12. JPasswordfield:

It is a text component designed specifically for password entry called a JPasswordField object. It is a small component that enables editing of a single line of text without displaying the original characters, even though the view shows that something was typed.

13. Imageicon:

ImageIcon increases Icon, Serializable, and Accessible are implemented by Object. a method of painting icons from images using the Icon interface. MediaTracker is used to monitor the loaded state of images that are created from a URL, filename, or byte array.

14. JFileChooser:

A component of the Java Swing package is JFileChooser. A component of JavaTM Foundation Classes (JFC) is the Swing package. Many features in JFC are useful for creating Java graphical user interfaces. Panel, dialogues, and buttons components, among others, are offered by Java Swing.

15. JSpinner:

The field in a JSpinner of this class is a single-line input that allows the user to choose an object value or a number from an ordered list.

Advantages of Java Swing:

Java platform-independent GUI applications can be created using Swing. It has a wide range of libraries that you can use to build stunning and reliable GUI applications. Other notable advantages of Java swing are:

  • Provides AWT-replacement components with both new functionalities and components.
  • Platform independence exists for Swing components.
  • The look and feel of swing parts can vary.
  • Swing components can offer a much more flexible UI because they use the Model-View-Controller paradigm (MVC).
  • Components of swings are compact (are less resource-intensive than AWT).
  • Double buffering is built into Swing.
  • Swing offers support for paint debugging when you create your own components.

Applications of Java Swing:

Java swing is a vital programming language and therefore has numerous applications ranging from the simple to complex tasks. Prominent applications are:

  • Swing is an API set ( API - Set Of Classes and Interfaces )
  • Swing is a tool offered for creating graphic user interfaces.
  • Swing is an AWT Extension Library (Abstract Window Toolkit)
  • includes updated and new components that have improved the appearance and usability of GUIs
  • Swing utilizes the model/view design architecture and can be used to create standalone swing GUI apps, servlets, and applets.
  • Swing is built on top of AWT and is more mobile and adaptable than AWT.
  • Swing is entirely written in Java, and Java Swing Components are lightweight and independent of platforms.
  • Pluggable-looking and -feeling swing supports Additionally, Swing offers more robust components like tables, lists, scroll panes, color selectors, tabbed panes, etc.
  • Additional Swing Adheres to MVC
  • Java swing has light weight components
  • It is platform independent

Java Swing Setup:

You'll need a text editor to create your Java programmes. Even more advanced IDEs are offered on the market. But for now, you might want to think about one of these:

  • Notepad: Use a simple text editor on a Windows computer like Notepad or TextPad.
  • Netbeans: Download the NetBeans Java IDE for free and open source at
  • Eclipse: is also an java IDE devised by the eclipse open source community and can be downloaded from

What is MVC Architecture:

The loosely based MVC architecture is followed by the architecture of the Swing API in the following ways:

  • A model represents the data of a component.
  • View is a visual representation of the data in the component.
  • The user's input on the view is captured by the controller, which updates the data of the component. Model is a separate element in the Swing component, and View and Controller are combined as User Interface elements. Swing has pluggable look-and-feel architecture because of this.

The three following factors are taken into account by every user interface:

  • UI elements: The primary visual components of a user interface are those that the user will eventually see and use. GWT offers a lengthy list of widely used and typical elements, ranging in complexity from simple to complex.
  • Layouts: They specify how UI elements should be arranged on the screen and give the GUI its final appearance and feel (Graphical User Interface).
  • Behavior: When a user interacts with UI elements, certain behaviors take place.
Also, Check out: "Core Java Interview Questions"

Creating Swing Applications:

First, Let us learn the Swing basics through this example i.e,
code for

public class HelloWorldSwing {
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
    private static void createAndShowGUI() {
        //Make sure we have nice window decorations.
        //Create and set up the window.
        JFrame frame = new JFrame("HelloWorldSwing");
        //Add the ubiquitous "Hello World" label.
        JLabel label = new JLabel("Hello World");
        //Display the window.
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {

One of the easiest Swing applications users can create is this one. Although it doesn't do much, the code exemplifies the fundamental code found in every Swing programme:

  1. Import the relevant packages, first.
  2. Set up an uppermost container.
  3. Show the container off.

Be thread safe.
The main Swing package is imported in the first line:

import javax.swing.*;

The only package that HelloWorldSwing requires is this one. The majority of Swing programmes must also import two AWT packages:

import java.awt.*;
import java.awt.event.*;

These packages are necessary because AWT infrastructure, such as the AWT event model, is used by Swing components. The event model controls how a component responds to actions like mouse and button movements.
There must be at least one top-level Swing container in every programme that uses the Swing GUI. The assistance that Swing components require for painting and event handling is provided by a top-level Swing container. There are three top-level Swing containers that are frequently used:

  1. JFrame
  2. Jdialog
  3. JApplet

The top-level container in the HelloWorldSwing example is a JFrame. A frame is a window that is implemented as an instance of the JFrame class and comes with default decorations like a border, a title, and buttons for iconizing and closing the window. Generally, GUI applications use at least one frame.

Here is the code that shows the frame:

JFrame frame = new JFrame("HelloWorldSwing");
The JComponent class is the ancestor of all Swing components, with the exception of top-level containers like JFrame. The text Hello World is displayed by the JComponent descendant JLabel in the HelloWorldSwing application.

These two lines of code create the frame before adding the JLabel component:

JLabel label = new JLabel("Hello World");

Notably, the label is added to the content pane of the frame rather than the frame itself. Every top-level container has a content pane that houses all of the components that are visible (aside from menus and window decorations) in the top-level container, either directly or indirectly.

We include the following code to cause the programme to end when the Close button is clicked:



Swing is a platform-neutral, lightweight component of Java's foundation class. It's employed to make window-based applications. It has elements such as a text field and scroll bar. The result of combining all these elements is a graphical user interface. This Java swing article should give you a brief and proper understanding of the software tool.

Course Schedule
Core Java TrainingJul 20 to Aug 04View Details
Core Java TrainingJul 23 to Aug 07View Details
Core Java TrainingJul 27 to Aug 11View Details
Core Java TrainingJul 30 to Aug 14View Details
Last updated: 04 Apr 2023
About Author

Kalla Saikumar is a technology expert and is currently working as a Marketing Analyst at MindMajix. Write articles on multiple platforms such as Tableau, PowerBi, Business Analysis, SQL Server, MySQL, Oracle, and other courses. And you can join him on LinkedIn and Twitter.

read less
  1. Share:
Java Articles