GUI Programming with Swing
Chapter 13: GUI Programming with Swing in Java
Java Swing is a part of Java Foundation Classes (JFC) that allows you to create rich graphical user interfaces (GUIs). It is built on top of AWT (Abstract Window Toolkit) but provides a more flexible and modern approach.
๐ Why Use Swing?
- Lightweight and platform-independent
- Highly customizable
- Supports pluggable look and feel
๐ Common Swing Components
JFrame– Main windowJPanel– Container to group componentsJButton– ButtonJLabel– Text displayJTextField– Single-line text inputJTextArea– Multi-line text input
๐ณ Example: Simple GUI
import javax.swing.*;
public class SimpleGUI {
public static void main(String[] args) {
JFrame frame = new JFrame("My First Swing App");
JButton button = new JButton("Click Me");
frame.add(button);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
๐ฆ Layout Managers
Layout Managers control the size and position of components.
FlowLayout– Left to right, line by lineBorderLayout– Divides into 5 regions (N, S, E, W, Center)GridLayout– Grid of equal-sized cells
๐️ Event Handling
Event handling is what makes GUIs interactive. You listen for user actions like button clicks or text input.
Example: Button Click Event
import javax.swing.*;
import java.awt.event.*;
public class EventDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Event Example");
JButton button = new JButton("Click Here");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked!");
}
});
frame.add(button);
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
๐งพ Summary Table
| Component | Purpose |
|---|---|
| JFrame | Main application window |
| JPanel | Panel to hold UI elements |
| JButton | Button for actions |
| JTextField | Single-line input |
| JTextArea | Multi-line input |
| JLabel | Displays text |
❓ FAQs with Answers:
Q1: What is Swing in Java?
A: Swing is a GUI widget toolkit for Java that provides a rich set of components for building window-based applications.
Q2: How is Swing different from AWT?
A: AWT is heavyweight and platform-dependent, while Swing is lightweight and more versatile. Swing components are written entirely in Java and provide better look and feel.
Q3: What is JFrame?
A: JFrame is the main window container used to create a GUI window in Swing.
Q4: How do you handle button click events in Swing?
A: By adding an ActionListener to the JButton using the addActionListener() method.
Q5: What is a layout manager in Swing?
A: Layout managers automatically position and size components within a container (e.g., FlowLayout, BorderLayout, GridLayout).
Q6: Can we add multiple panels in a JFrame?
A: Yes, you can add multiple JPanel instances to a JFrame and organize them using layout managers.
Q7: Is Swing still used in modern Java apps?
A: While newer frameworks like JavaFX exist, Swing is still widely used for lightweight and educational GUI applications.
๐ Conclusion
Swing empowers Java developers to build feature-rich, responsive desktop applications. With proper use of layout managers and event handling, creating interactive programs becomes easy and powerful.
๐ Related Links: Complete Java Course
- Chapter 1: Introduction to Java
- Chapter 2: Java Environment Setup
- Chapter 3: Variables and Data Types
- Chapter 4: Operators in Java
- Chapter 5: Control Statements
- Chapter 6: Loops in Java
- Chapter 7: Arrays and Strings
- Chapter 8: Object-Oriented Programming (OOP)
- Chapter 9: Inheritance and Polymorphism
- Chapter 10: Exception Handling
- Chapter 11: Multithreading
- Chapter 12: File Handling in Java
- Chapter 13: Applets and GUI (AWT/Swing)
- Chapter 14: Java Project Ideas & Practice Questions

Comments
Post a Comment