For working professionals
For fresh graduates
More
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
Foreign Nationals
The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not .
Recommended Programs
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
46. Packages in Java
53. Java Collection
56. Generics In Java
57. Java Interfaces
60. Streams in Java
63. Thread in Java
67. Deadlock in Java
74. Applet in Java
75. Java Swing
76. Java Frameworks
78. JUnit Testing
81. Jar file in Java
82. Java Clean Code
86. Java 8 features
87. String in Java
93. HashMap in Java
98. Enum in Java
101. Hashcode in Java
105. Linked List in Java
109. Array Length in Java
111. Split in java
112. Map In Java
115. HashSet in Java
118. DateFormat in Java
121. Java List Size
122. Java APIs
128. Identifiers in Java
130. Set in Java
132. Try Catch in Java
133. Bubble Sort in Java
135. Queue in Java
142. Jagged Array in Java
144. Java String Format
145. Replace in Java
146. charAt() in Java
147. CompareTo in Java
151. parseInt in Java
153. Abstraction in Java
154. String Input in Java
156. instanceof in Java
157. Math Floor in Java
158. Selection Sort Java
159. int to char in Java
164. Deque in Java
172. Trim in Java
173. RxJava
174. Recursion in Java
175. HashSet Java
177. Square Root in Java
190. Javafx
Java is a widely used programming language for building desktop, mobile, web, and cloud applications. Known for speed, security, and reliability, it also offers powerful libraries for creating GUIs.
This Java Swing Tutorial covers AWT and Swing in Java, the toolkits for designing interactive interfaces. Swing, a lightweight and platform-independent library, extends AWT with advanced components like tables, lists, scroll panes, and tabbed panes. With its model/view architecture, Swing makes GUI development flexible and user-friendly.
Strengthen your understanding of Java Swing with upGrad’s Software Engineering courses! Learn everything from basic Java to advanced frameworks, preparing you for the future of tech. Join now and explore endless opportunities.
Initially, a brief idea about Java and Swing is presented. After that complete information about swing is explained which covers various topics including AWT, swing, Java foundation class, the hierarchy of Java Swing Classes, commonly used methods of component, features of swing class, swing classes hierarchy, the MVC connection, etc. Different types of examples are explained to give a clear idea about Java swing, swing by association inside the constructor, swing by inheritance, etc.
1. The Abstract Window Toolkit (AWT) is an application programming interface that supports a graphical user interface (GUI) or window-based applications in Java. Swing belongs to the Java foundation class library and it is also an extension of AWT. Swing is used to develop Java-based front-end GUI applications.
2. Swing supports much more improved functionality than AWT. It supports new components, expanded component features, and exceptional event-handling features which include drag-and-drop support.
In 2025, professionals who can use advanced Java programming techniques like Swings to streamline business operations will be in high demand. If you're looking to develop skills in programming, here are some top-rated courses to help you get there:
3. Components of AWT are heavily weighted whereas components of AWT are light-weighted.
4. AWT is platform-dependent and swing is platform-independent.
5. Swing has much more advanced components whereas the components of AWT are not that much advanced.
6. Execution speed of the swing is faster and the execution speed of the AWT is not much faster.
7. Swing supports a pluggable look and feel whereas AWT does not support it.
8. Swing supports MVC patterns.
9. Java Swing Components require more memory space whereas components of AWT don't require much memory space..
Check out this: Top 10 Free Java Courses with Certificates for In-Demand Java Jobs
JFC stands for Java Foundation Classes. JFC supports a wide range of GUI components and services. This makes the development and deployment of desktop and internet applications easier. JFC supports swing, cut and paste, accessibility features, desktop color features, JAVA- 2D, etc. Java swing tutorial is a segment of JFC that is used in developing window-based applications.
The hierarchy of the Java swing w3schools begins with the component class. Different types of user interface elements such as buttons, levels, and text fields are derived from the Java swing class.
Some methods of a component class are listed below.
1. public void add(Component c): This method is used to add two components.
2. public void setSize(int width,int height): This method is used to assign the size of a component. This method sets the width and height of the component.
3. public void setLayout(LayoutManager m): This method is used to assign the layout manager.
4. public void setVisible(boolean b): This method sets the visibility of the component.
Also Read: How to Code, Compile, and Run Java Projects: A Beginner’s Guide
Example: A simple Java swing program
import javax.swing.*;
public class SwingExample
{
public static void main(String[] args)
{
JFrame f=new JFrame();
JButton b=new JButton("click");
b.setBounds(120,50,50, 40);
f.add(b);
f.setSize(200,300);
f.setLayout(null);
f.setVisible(true);
}
}
Explanation:
1. JFrame and JButton are created.
2. Values of all the bounds such as x-axis, y-axis, width, and height are set.
3. JButton is added in JFrame.
4. The class JButton creates a labeled button with platform-independent implementation. It is used for the implementation of a push button.
JFrame provides a frame in the specific graphics configuration of the screen
Example: A program to create three buttons with captions PAST, PRESENT, and FUTURE.
import java.awt.*;
class button
{
button()
{
Frame f = new Frame();
Button b1 = new Button("PAST");
b1.setBounds(110, 75, 75, 60);
f.add(b1);
Button b2 = new Button("PRESENT");
b2.setBounds(110, 120, 40, 80);
f.add(b2);
Button b3 = new Button("FUTURE");
b3.setBounds(110, 120, 60, 40);
f.add(b3);
f.setSize(250, 250);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String a[])
{
new button();
}
}
Example : A program to create three buttons with captions ODISHA, CHHATTISGARH, BIHAR
import java.awt.*;
class Lan {
Lan() {
Frame f = new Frame();
Label l1 = new Label("Select your state");
l1.setBounds(110, 60, 140, 100);
f.add(l1);
Checkbox c2 = new Checkbox("ODISHA");
c2.setBounds(110, 100, 80, 80);
f.add(c2);
Checkbox c3 = new Checkbox("CHHATTISGARH");
c3.setBounds(100, 120, 70, 50);
f.add(c3);
Checkbox c4 = new Checkbox("BIHAR");
c4.setBounds(120, 150, 60, 80);
f.add(c4);
f.setSize(250, 250);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String ar[]) {
new Lan();
}
}
Example: Create a program using Java constructor
import javax.swing.*;
public class Simple
{
JFrame f;
Simple()
{
f=new JFrame();
JButton b=new JButton("click");
b.setBounds(120,110,100, 50);
f.add(b);
f.setSize(300,500);//300 width and 500 height
f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
}
public static void main(String[] args) {
new Simple();
}
}
Example : Create a program using inheritance
import javax.swing.*;
public class Simple2 extends JFrame
{
JFrame g;
Simple2()
{
JButton c=new JButton("click");
c.setBounds(120,100,110, 50);
add(c);
setSize(200,300);
setLayout(null);
setVisible(true);
}
public static void main(String[] args)
{
new Simple2();
}
}
1. Lightweight components: A swing component is known as a lightweight component as it is completely written in Java. It can do the high-level display work itself. It does not rely on any non java system classes or code provided by the operating system. The components have their specific view supported by Java’s look and feel classes.
2. Pluggable look and feel: Here Look means the appearance of GUI widgets and feel denotes the way the widgets perform. The pluggable look and feel feature is a mechanism that permits the change of the look and feel of the GUI at runtime. Wherever the program runs this feature allows the components’ look and feel to remain the same. This offers flexibility in choosing the look and feel of the GUI of an application.
3. Highly customizable: The visual appearance does not depend upon the internal presentation. So, the swing controls can be easily customized.
4. Rich controls: Developed controls like Tree tabbedpane, slider, colorpicker, and table controls are provided.
Also Read: Java Classes and Objects
The MVC Connection
The model view controller (MVC) is a prominent design pattern that is used in the web development world. This method is used to organize the code. Three logical components of MVC are the model, the view, and the controller. Every component handles certain development aspects of a robust structure for web applications. MVC states that a program or application must consist of three features. The 'model' term signifies state information linked to the component including all the data and related logic, the terminology 'view' indicates the look of the component on the screen and it also handles user interaction, and the 'controller' term shows, how the component reacts to the user or, this is an interface between model and view components.
Also Read: What is MVC Architecture in Java? Explained
This Java Swing Tutorial introduces the core concepts of Swing and its role in building GUIs. It explains the difference between AWT and Swing in Java, defines the Java Foundation Class, and illustrates the Swing hierarchy. You’ll also explore commonly used component methods like add(), setSize(), setLayout(), and setVisible(). Finally, simple examples with Java Swing in IntelliJ are provided to give you a clear hands-on understanding.
Java is a widely-used general-purpose programming language suitable for building mobile, desktop, and web applications, as well as server-side technologies, cloud computing solutions, and big data processing. Its platform independence, strong security features, and vast library ecosystem make it a preferred choice for developers globally. Java serves as the foundation for building GUI applications using frameworks such as Swing and AWT, which are covered in this Java Swing Tutorial.
Java Swing is a lightweight GUI toolkit within the Java Foundation Classes (JFC) used for building platform-independent desktop applications. It provides a rich set of components like buttons, labels, text fields, tables, and lists, while supporting model-view-controller (MVC) architecture. Swing’s enhanced functionality over AWT, combined with its pluggable look-and-feel and customizability, makes it central to this Java Swing Tutorial.
The Abstract Window Toolkit (AWT) is Java’s original GUI framework that provides heavyweight components relying on native OS widgets. While functional, AWT lacks the flexibility and advanced features of Swing. Swing builds upon AWT, offering lightweight, platform-independent components and improved UI capabilities, which we explore in this Java Swing Tutorial.
Java is the programming language, while Swing is a framework within Java used to create graphical user interfaces. Swing extends AWT, providing enhanced functionality such as lightweight components, expanded event handling, MVC architecture, and a pluggable look-and-feel. Understanding this distinction is fundamental in this Java Swing Tutorial for designing sophisticated desktop applications.
Java Foundation Classes (JFC) encompass Swing, AWT, and other APIs that facilitate the creation of rich GUI applications in Java. JFC includes prebuilt components, layouts, and event-handling mechanisms, enabling developers to quickly develop interactive and visually appealing applications. This Java Swing Tutorial highlights JFC’s role in simplifying GUI design.
Swing features lightweight, platform-independent components, a pluggable look-and-feel, rich customization options, and robust event-handling. Its design supports MVC architecture, enabling separation of data, presentation, and control logic. Swing also incorporates double buffering for smooth rendering, which is emphasized in this Java Swing Tutorial.
The Model-View-Controller (MVC) pattern organizes application code by separating the data model, UI view, and controller logic. Swing leverages MVC to improve maintainability and flexibility in GUI applications. This structure allows developers to modify the interface independently from the underlying data logic, a concept thoroughly explored in this Java Swing Tutorial.
javax.swing is the package containing the majority of Swing’s lightweight GUI components, including JButton, JTextField, JLabel, JPanel, and JFrame. It provides a consistent and extendable framework for creating modern Java desktop applications. This Java Swing Tutorial uses javax.swing components extensively to demonstrate practical GUI development.
JButton is a Swing component for creating push buttons with labels, icons, or both. It supports action listeners, enabling developers to respond to user interactions. This Java Swing Tutorial demonstrates using JButton for building interactive forms and interfaces efficiently.
JFrame provides the primary window for a Swing application, serving as a container for other components. It can be customized with layouts, menus, and event handling. Understanding JFrame is crucial for constructing structured GUIs, as highlighted in this Java Swing Tutorial.
The EDT is the dedicated thread responsible for rendering Swing components and processing user events like clicks and keypresses. All UI updates should occur on the EDT to prevent inconsistent rendering or freezing. Long-running tasks should run on separate threads while updates return via SwingUtilities.invokeLater(). This mechanism is vital for responsive applications covered in this Java Swing Tutorial.
No. Swing is not inherently thread-safe, so updating GUI components outside the EDT may cause race conditions or visual inconsistencies. Developers must use the EDT or helper methods like SwingUtilities.invokeLater() to perform UI updates safely. Thread management is a key topic in this Java Swing Tutorial.
Swing components are lightweight because they are rendered entirely in Java, not relying on native OS widgets. This makes them more flexible and platform-independent compared to AWT components, which are heavyweight and OS-dependent. Swing’s lightweight design also supports pluggable look-and-feel and uniform behavior across platforms, a core focus of this Java Swing Tutorial.
All Swing components inherit from JComponent, which extends AWT’s Container. Swing follows the MVC pattern, separating the model (data), view (UI), and controller (logic). This structure simplifies maintenance and enhances flexibility, a concept emphasized in this Java Swing Tutorial.
Double buffering improves rendering performance by drawing graphics to an off-screen buffer before displaying them on screen. This technique eliminates flickering during animations or complex GUI updates, which is demonstrated in examples throughout this Java Swing Tutorial.
SwingWorker allows developers to run intensive tasks in the background while keeping the GUI responsive. Heavy processing occurs in doInBackground(), and results are safely returned to the EDT via done(). SwingWorker is essential for responsive GUI design, explained in this Java Swing Tutorial.
Custom rendering is done by overriding paintComponent(Graphics g) in Swing components. Developers use Graphics2D to draw shapes, apply colors, or perform transformations. This enables creation of charts, animations, or custom controls, an advanced topic in this Java Swing Tutorial.
JComponent is the superclass of most Swing components. It provides essential functionality such as painting, double buffering, property change notifications, and key bindings. Its centrality in Swing makes it the foundation for building complex GUIs, a key subject in this Java Swing Tutorial.
The pluggable look-and-feel system lets developers change the appearance of Swing applications at runtime. Swing can mimic native OS styles or use custom themes. This feature ensures consistent cross-platform aesthetics, a highlight of this Java Swing Tutorial.
Swing supports accessibility through the Java Accessibility API, enabling compatibility with screen readers, keyboard navigation, and other assistive tools. Components expose accessible interfaces that assistive technologies can query, ensuring applications are usable by all users. This is covered in detail in this Java Swing Tutorial.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
FREE COURSES
Start Learning For Free
Author|900 articles published