Beginner Reference Manual • Chapter 8

Building Desktop Apps with Windows & Buttons (Java Swing GUI)

Learn how to build graphical desktop windows, buttons, text fields, labels, layout managers, and respond to user clicks using event listeners.

Until now, our programs interacted through plain console text. Now it's time to build real desktop applications with windows, buttons, text boxes, and click events!

8.1. 1. Swing Components & Window Frames

Definition: Swing provides graphical user interface (GUI) components for building desktop applications in Java.

Core Working Principles:

graph TD
    Window[JFrame: Main Window Frame] --> Panel[JPanel: Content Container]
    Panel --> Component1[JLabel: 'Enter Name:']
    Panel --> Component2[JTextField: Text Box]
    Panel --> Component3[JButton: 'Submit Button']
                    
Architecture Specification 8.1: Desktop GUI Window & Component Layout Structure
5-Marks Exam Scoring Points
  • JFrame creates top-level desktop windows.
  • JPanel acts as an intermediate container for grouping components.
  • Swing components are written entirely in Java (lightweight).

8.2. 2. Organizing Screen Layouts (Layout Managers)

Definition: Layout managers automatically organize and position components within windows.

Core Working Principles:

Layout Manager Arrangement Style Best Use Case
BorderLayout 5 Zones (North, South, East, West, Center) Main Window Shells
FlowLayout Left-to-Right rows Button bars & Toolbar rows
GridLayout Equal-sized grid rows & columns Calculator keypads & Form inputs
5-Marks Exam Scoring Points
  • BorderLayout positions components in 5 regions (North, South, East, West, Center).
  • FlowLayout lays out components in horizontal rows.
  • GridLayout arranges components in equal grid cells.

8.3. 3. Responding to Button Clicks (Delegation Event Model)

Definition: Swing uses event listeners to execute Java code when a user interacts with screen components.

Core Working Principles:

🔔 Layman Analogy: Home Doorbell & Visitor Ring

• Doorbell (Event Source): The push button outside the house.
• Ring Sound (ActionEvent): Generated when a visitor presses the button.
• Homeowner (ActionListener): Listens for the ring and opens the front door!

JFrame frame = new JFrame("My Desktop App");
JButton btn = new JButton("Click Me");

// Adding an ActionListener to respond to clicks
btn.addActionListener(e -> {
    System.out.println("Button was clicked!");
});

frame.add(btn);
frame.setSize(300, 200);
frame.setVisible(true);
5-Marks Exam Scoring Points
  • Delegation Event Model separates UI components from event listener logic.
  • ActionListener receives button click events.
  • Call setVisible(true) to display Swing windows.

✍️ Chapter 8 Reference Exercises & Review Problems

Exercise 8.1: Name 3 basic Swing components and explain what each one does on screen.
Step-by-Step Explanation for Beginners:
1. **`JFrame`:** The main window box on your computer screen with title bar and close button.
2. **`JButton`:** A clickable push button that triggers actions when pressed.
3. **`JLabel`:** A simple display box used to display text messages or image icons to the user!
Exercise 8.2: How does an `ActionListener` know when a button has been clicked?
Step-by-Step Explanation for Beginners:
When you register an `ActionListener` on a `JButton` using `btn.addActionListener()`, Java connects the button to your listener. When the user clicks the button, Swing automatically creates an `ActionEvent` object and invokes your listener's callback method!