GUI Programming with Swing

Chapter 13: GUI Programming with Swing in Java

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 window
  • JPanel – Container to group components
  • JButton – Button
  • JLabel – Text display
  • JTextField – Single-line text input
  • JTextArea – 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 line
  • BorderLayout – 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

ComponentPurpose
JFrameMain application window
JPanelPanel to hold UI elements
JButtonButton for actions
JTextFieldSingle-line input
JTextAreaMulti-line input
JLabelDisplays 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

Comments

Popular posts from this blog

Best AI Tools for Students and Professionals in 2025 | Boost Learning & Productivity

Introduction to Java

Top AI Tools for Content Creators: Bloggers, YouTubers & Writers (2025)