π Introduction
In previous blogs, we explored AWT (Abstract Window Toolkit) and built basic GUI applications πͺ
π But AWT has some limitations:
- Platform-dependent β
- Limited components β
- Less flexible β
To overcome these, Java introduced Swing π―
π Swing is a more powerful, flexible, and modern GUI toolkit built on top of AWT.
π§ What is Swing?
π Swing is part of the javax.swing package and is used to create rich GUI applications.
π₯ Key Features of Swing
- Platform-independent π
- Lightweight components β‘
- Rich set of components π¨
- Customizable Look & Feel π―
βοΈ AWT vs Swing (VERY IMPORTANT)
| Feature | AWT | Swing |
|---|---|---|
| Package | java.awt | javax.swing |
| Components | Heavyweight | Lightweight |
| Look & Feel | OS dependent | Platform independent |
| Components Available | Limited | Rich |
| Customization | Less | High |
π§ Key Understanding
π AWT uses native OS components
π Swing draws components itself (Java-based)
π§± Basic Structure of Swing Program
β First Swing Program (Complete Runnable Code)
import javax.swing.*;
public class FirstSwingApp {
public static void main(String[] args) {
JFrame frame = new JFrame("My First Swing App");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
π Explanation
JFrameβ main window πͺsetSize()β sets window sizesetDefaultCloseOperation()β closes program properly βsetVisible(true)β displays window
π§© Swing Components
πΉ 1. JButton
import javax.swing.*;
public class ButtonSwing {
public static void main(String[] args) {
JFrame frame = new JFrame("Button Example");
JButton button = new JButton("Click Me");
frame.add(button);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
π Explanation
JButtonβ clickable button π- Works similar to AWT but more flexible
πΉ 2. JLabel
import javax.swing.*;
public class LabelSwing {
public static void main(String[] args) {
JFrame frame = new JFrame("Label Example");
JLabel label = new JLabel("Welcome to Swing");
frame.add(label);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
πΉ 3. JTextField
import javax.swing.*;
public class TextFieldSwing {
public static void main(String[] args) {
JFrame frame = new JFrame("TextField Example");
JTextField tf = new JTextField(15);
frame.setLayout(new java.awt.FlowLayout());
frame.add(tf);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
π οΈ Mini Swing Application (Input + Processing)
π§ͺ Even/Odd Checker in Swing
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SwingEvenOdd {
public static void main(String[] args) {
JFrame frame = new JFrame("Even Odd Checker");
frame.setLayout(new FlowLayout());
JTextField tf = new JTextField(10);
JButton btn = new JButton("Check");
JLabel result = new JLabel("");
frame.add(new JLabel("Enter Number:"));
frame.add(tf);
frame.add(btn);
frame.add(result);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int num = Integer.parseInt(tf.getText());
if (num % 2 == 0) {
result.setText("Even Number β
");
} else {
result.setText("Odd Number π₯");
}
} catch (Exception ex) {
result.setText("Invalid Input β");
}
}
});
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
π Explanation
- Same logic as AWT
- Better UI and control
setDefaultCloseOperation()removes need for WindowAdapter
β οΈ Common Mistakes Students Make
β Forgetting setDefaultCloseOperation()
π Window closes but program keeps running
β Mixing AWT and Swing improperly
π Can cause UI issues
β Not setting layout
π Components may overlap
β Forgetting setVisible(true)
π Window will not appear
π€― Common Confusions
π€ Why Swing is called Lightweight?
π Does NOT depend on OS components
π€ Is Swing better than AWT?
π Yes β more flexible and powerful
π€ Can we use AWT with Swing?
π Yes, but not recommended
πΌ Interview Questions
β What is Swing?
π Java GUI toolkit with lightweight components
β Difference between AWT and Swing?
π AWT β Heavyweight
π Swing β Lightweight
β What is JFrame?
π Top-level container in Swing
β Why Swing is platform-independent?
π Components are written in Java
π― Best Practices
βοΈ Always use Swing over AWT
βοΈ Use proper layouts
βοΈ Handle events properly
βοΈ Keep UI simple
π Conclusion
Swing is the modern solution for GUI development in Java π
In this blog, you learned:
- βοΈ What Swing is
- βοΈ Differences between AWT and Swing
- βοΈ Swing components
- βοΈ Building real applications
π‘ Final Thought
π If AWT is the foundation,
π Swing is the real-world tool πͺ
π₯ One-Line Summary
π βSwing provides powerful, flexible, and platform-independent GUI components in Java.β
π» Happy Coding! π
Next step: Event Delegation Model (deep dive) π₯