0% found this document useful (0 votes)
13 views10 pages

Java Swing

Java Swing is an extension of the Abstract Window Toolkit (AWT) and part of the Java Foundation Classes (JFC), providing enhanced functionality and a wider range of user interface components for developing complex graphical user interfaces. It features lightweight, platform-independent components, supports a pluggable look and feel, and employs the Model-View-Controller (MVC) architecture. Swing offers advanced controls such as tables and color choosers, making it a preferred choice for modern Java GUI applications.

Uploaded by

P Sakthikumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views10 pages

Java Swing

Java Swing is an extension of the Abstract Window Toolkit (AWT) and part of the Java Foundation Classes (JFC), providing enhanced functionality and a wider range of user interface components for developing complex graphical user interfaces. It features lightweight, platform-independent components, supports a pluggable look and feel, and employs the Model-View-Controller (MVC) architecture. Swing offers advanced controls such as tables and color choosers, making it a preferred choice for modern Java GUI applications.

Uploaded by

P Sakthikumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

17/05/2025, 20:09 Introduction to Java Swing | GeeksforGeeks

Search... 99+

Introduction to Java Swing


Last Updated : 30 Jul, 2024

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.

Introduction of Java Swing

Swing has about four times the number of User Interface [UI]
components as AWT and is part of the standard Java distribution. By
today's application GUI requirements, AWT is a limited implementation,
not quite capable of providing the components required for developing
complex GUIs required in modern commercial applications. The AWT
component set has quite a few bugs and does take up a lot of system
resources when compared to equivalent Swing resources. Netscape
introduced its Internet Foundation Classes [IFC] library for use with
Java. Its Classes became very popular with programmers creating GUI's
for commercial applications.

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 (Develop) 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.

https://www.geeksforgeeks.org/introduction-to-java-swing/ 1/14
17/05/2025, 20:09 Introduction to Java Swing | GeeksforGeeks

Java Swing Components are Platform-independent, and The Swing


Components are lightweight.
Swing Supports a Pluggable look and feel and Swing provides more
powerful components.
such as tables, lists, Scrollpanes, Colourchooser, tabbed pane, etc.
Further Swing Follows MVC.

Difference between Java Swing and Java AWT


There are certain points from which Java Swing is different than Java
AWT as mentioned below:

Java AWT Java Swing

Swing is a part of Java Foundation


Java AWT is an API to develop GUI
Classes and is used to create
applications in Java.
various applications.

Components of AWT are heavy The components of Java Swing are


weighted. lightweight.

Components are platform Components are platform


dependent. independent.

Execution Time is more than Swing. Execution Time is less than AWT.

AWT components require java.awt Swing components requires


package. javax.swing package.

To know more about the topic, refer to Java Swing vs Java AWT.

What is JFC?
JFC stands for Java Foundation Classes. JFC is the set of GUI
components that simplify desktop Applications. Many programmers
think that JFC and Swing are one and the same thing, but that is not so.

https://www.geeksforgeeks.org/introduction-to-java-swing/ 2/14
17/05/2025, 20:09 Introduction to Java Swing | GeeksforGeeks

JFC contains Swing [A UI component package] and quite a number of


other items:

Cut and paste: Clipboard support.


Accessibility features: Aimed at developing GUIs for users with
disabilities.
The Desktop Colors Features were first introduced in Java 1.1
Java 2D: it has Improved colors, images, and text support.

Features Of Swing Class


Pluggable look and feel.
Uses MVC architecture.
Lightweight Components
Platform Independent
Advanced features such as JTable, JTabbedPane, JScollPane, etc.
Java is a platform-independent language and runs on any client
machine, the GUI look and feel, owned and delivered by a platform-
specific O/S, simply does not affect an application's GUI constructed
using Swing components.
Lightweight Components: Starting with the JDK 1.1, its AWT-
supported lightweight component development. For a component to
qualify as lightweight, it must not depend on any non-Java [O/s
based) system classes. Swing components have their own view
supported by Java's look and feel classes.
Pluggable Look and Feel: This feature enable the user to switch the
look and feel of Swing components without restarting an application.
The Swing library supports components' look and feels that remain
the same across all platforms wherever the program runs. The Swing
library provides an API that gives real flexibility in determining the
look and feel of the GUI of an application
Highly customizable - Swing controls can be customized in a very
easy way as visual appearance is independent of internal
representation.
Rich controls- Swing provides a rich set of advanced controls like
Tree TabbedPane, slider, colorpicker, and table controls.

https://www.geeksforgeeks.org/introduction-to-java-swing/ 3/14
17/05/2025, 20:09 Introduction to Java Swing | GeeksforGeeks

Swing Classes Hierarchy

Java Course Java Arrays Java Strings Java OOPs Java Collection Java 8 Tutorial Java Multith

The MVC Connection


In general, a visual component is a composite of three distinct
aspects:
1. The way that the component looks when rendered on the screen.
2. The way such that the component reacts to the user.
3. The state information associated with the component.

Over the years, one component architecture has proven itself to be


exceptionally effective: - Model-View-Controller or MVC for short.
In MVC terminology, the model corresponds to the state information
associated with the Component.
The view determines how the component is displayed on the screen,
including any aspects of the view that are affected by the current
state of the model.
The controller determines how the component reacts to the user.

The simplest Swing components have capabilities far beyond AWT


components as follows:

Swing buttons and labels can be displaying images instead of or in


addition to text.
The borders around most Swing components can be changed easily.
For example, it is easy to put a 1-pixel border around the outside of a
Swing label.
Swing components do not have to be rectangular. Buttons, for
example, can be round.

https://www.geeksforgeeks.org/introduction-to-java-swing/ 4/14
17/05/2025, 20:09 Introduction to Java Swing | GeeksforGeeks

Now The Latest Assertive technologies such as screen readers can


easily get information from Swing components. Example: A screen
reader tool can easily capture the text that is displayed on a Swing
button or label.

Example of Java Swing Programs


Example 1: Develop a program using label (swing) to display the message “GFG
WEB Site Click”:

// Java program using label (swing)


// to display the message “GFG WEB Site Click”
import java.io.*;
import javax.swing.*;

// Main class
class GFG {

// Main driver method


public static void main(String[] args)
{
// Creating instance of JFrame
JFrame frame = new JFrame();

// Creating instance of JButton


JButton button = new JButton(" GFG WebSite Click");

// x axis, y axis, width, height


button.setBounds(150, 200, 220, 50);

// adding button in JFrame


frame.add(button);

// 400 width and 500 height


frame.setSize(500, 600);

// using no layout managers


frame.setLayout(null);

// making the frame visible


frame.setVisible(true);
}
}

Output:

https://www.geeksforgeeks.org/introduction-to-java-swing/ 5/14
17/05/2025, 20:09 Introduction to Java Swing | GeeksforGeeks

Example 2: Write a program to create three buttons with caption OK, SUBMIT,
CANCEL.

// Java program to create three buttons


// with caption OK, SUBMIT, CANCEL
import java.awt.*;

class button {
button()
{
Frame f = new Frame();

// Button 1 created
// OK button
Button b1 = new Button("OK");
b1.setBounds(100, 50, 50, 50);
f.add(b1);

// Button 2 created
// Submit button
Button b2 = new Button("SUBMIT");
b2.setBounds(100, 101, 50, 50);
f.add(b2);

// Button 3 created
// Cancel button
Button b3 = new Button("CANCEL");
b3.setBounds(100, 150, 80, 50);
f.add(b3);

f.setSize(500, 500);
f.setLayout(null);
f.setVisible(true);
}

public static void main(String a[]) { new button(); }


}

Output:

https://www.geeksforgeeks.org/introduction-to-java-swing/ 6/14
17/05/2025, 20:09 Introduction to Java Swing | GeeksforGeeks

Output of Example 2

Example 3: Program to Add Checkbox in the Frame

// Java Swing Program to Add Checkbox


// in the Frame
import java.awt.*;

// Driver Class
class Lan {
// Main Function
Lan()
{
// Frame Created
Frame f = new Frame();

Label l1 = new Label("Select known Languages");

l1.setBounds(100, 50, 120, 80);


f.add(l1);

// CheckBox created
Checkbox c2 = new Checkbox("Hindi");
c2.setBounds(100, 150, 50, 50);
f.add(c2);

// CheckBox created
Checkbox c3 = new Checkbox("English");
c3.setBounds(100, 200, 80, 50);
f.add(c3);

// CheckBox created
Checkbox c4 = new Checkbox("marathi");
c4.setBounds(100, 250, 80, 50);
f.add(c4);

f.setSize(500, 500);
f.setLayout(null);
f.setVisible(true);
}

public static void main(String ar[]) { new Lan(); }


}

https://www.geeksforgeeks.org/introduction-to-java-swing/ 7/14
17/05/2025, 20:09 Introduction to Java Swing | GeeksforGeeks

Output:

Output of Example 3

Components of Swing Class the task's percentage

Class Description

A Component is the Abstract base class for about the


non-menu user-interface controls of Java SWING.
Component
Components are representing an object with a
graphical representation.

A Container is a component that can container Java


Container
SWING Components

A JComponent is a base class for all swing UI


Components In order to use a swing component that
JComponent inherits from JComponent, the component must be in a
containment hierarchy whose root is a top-level Java
Swing container.

A JLabel is an object component for placing text in a


JLabel
container.

JButton This class creates a labeled button.

https://www.geeksforgeeks.org/introduction-to-java-swing/ 8/14
17/05/2025, 20:09 Introduction to Java Swing | GeeksforGeeks

Class Description

JColorChooser A JColorChooser provides a pane of controls designed


to allow the user to manipulate and select a color.

A JCheckBox is a graphical (GUI) component that can


JCheckBox
be in either an on-(true) or off-(false) state.

The JRadioButton class is a graphical (GUI) component


JRadioButton that can be in either an on-(true) or off-(false) state. in
the group

A JList component represents the user with the


JList
scrolling list of text items.

A JComboBox component is Presents the User with a


JComboBox
show up Menu of choices.

A JTextField object is a text component that will allow


JTextField
for the editing of a single line of text.

JPasswordField A JPasswordField object it is a text component


specialized for password entry.

A JTextArea object is a text component that allows for


JTextArea
the editing of multiple lines of text.

A ImageIcon control is an implementation of the Icon


Imagelcon
interface that paints Icons from Images

A JScrollbar control represents a scroll bar component


JScrollbar
in order to enable users to Select from range values.

JOptionPane provides set of standard dialog boxes that


JOptionPane
prompt users for a value or Something.

https://www.geeksforgeeks.org/introduction-to-java-swing/ 9/14
17/05/2025, 20:09 Introduction to Java Swing | GeeksforGeeks

Class Description

A JFileChooser it Controls represents a dialog window


JFileChooser
from which the user can select a file.

As the task progresses towards completion, the


JProgressBar
progress bar displays the tasks percentage on its
completion.

A JSlider this class is letting the user graphically (GUI)


JSlider select by using a value by sliding a knob within a
bounded interval.

A JSpinner this class is a single line input where the


JSpinner field that lets the user select by using a number or an
object value from an ordered sequence.

Comment More info


Next Article
Campus Training Program Introduction to Spring Framework

Similar Reads
Introduction to Processing | Java
Processing is an open-source programming language and development
environment that is built on top of the Java programming language. It is…

15+ min read

Introduction to Java
Java is a class-based, object-oriented programming language that is
designed to have as few implementation dependencies as possible. It is…

15+ min read

https://www.geeksforgeeks.org/introduction-to-java-swing/ 10/14

You might also like