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:
- Core Desktop Components:
- •
JFrame: The main desktop window with a title bar, minimize, and close buttons. - •
JPanel: A container panel used to group components inside a window. - •
JButton: A push button users can click. - •
JLabel: A display area for text or images. - •
JTextField: A box where users can type text.
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']
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:
- Common Layout Managers:
- •
BorderLayout: Arranges items in 5 spatial zones (North, South, East, West, Center). Default forJFrame. - •
FlowLayout: Places components side-by-side in a row, wrapping to the next line. Default forJPanel. - •
GridLayout: Arranges components in an equal grid of rows and columns.
| 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:
- Event Source: The button or component generating user actions.
- ActionListener: The interface receiving click notifications.
- ActionEvent: The event object created when a button is clicked.
• 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
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!
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!