Unit 5 · Lesson 115 minAcademic review pending

Building an actual window, at last

Everything from Unit IV — classes, listeners, layouts — comes together here. Swing gives you the actual visible pieces: windows, labels, buttons, checkboxes, all built from what you already know.

Choose explanation

After this lesson

You should be able to

  • Explain why Swing replaced AWT as the preferred way to build Java GUIs.
  • Build a simple window with a label, a text field, and a button.
  • Describe MVC at a high level, as it relates to a Swing component.
01

Why Swing, not AWT

AWT, Java's original GUI toolkit, delegated drawing to the underlying operating system, which meant a button looked slightly different on Windows, Linux, and older systems — and AWT's component set was limited to the simplest controls. Swing components are drawn entirely by Java itself, look identical on every platform, and include a much richer set of controls: tables, trees, tabbed panes, none of which AWT ever had.

This is why the syllabus and this unit focus on Swing: it is the practical, modern choice, built on top of AWT's foundational ideas like event handling from Unit IV, which is why everything you learned about listeners there applies directly to every Swing component here.

02

A window built from familiar pieces

A JFrame is the actual window. Every visible piece placed inside it — JLabel for static text, JTextField for editable text, JButton for a clickable action, JCheckBox and JRadioButton for choices — extends JComponent, the same is-a relationship style from Unit II, just applied to a family of visible widgets instead of a Student or Shape hierarchy.

Putting it together uses exactly what you already know: create the components, add() them to a container using a layout manager from Unit IV, attach listeners to the ones that need to react to the user, and call setVisible(true) to actually show the window.

Components, a layout, a listener — everything from Unit IV, together
import javax.swing.*;
import java.awt.FlowLayout;

JFrame frame = new JFrame("Greeting App");
frame.setLayout(new FlowLayout());
frame.setSize(300, 150);

JLabel nameLabel = new JLabel("Enter your name:");
JTextField nameField = new JTextField(15);
JButton greetButton = new JButton("Greet");

greetButton.addActionListener(e ->
    JOptionPane.showMessageDialog(frame, "Hello, " + nameField.getText() + "!"));

frame.add(nameLabel);
frame.add(nameField);
frame.add(greetButton);
frame.setVisible(true);

Try it yourself

Build a window with a JCheckBox labelled "Subscribe" and a JButton labelled "Submit". When Submit is clicked, print whether the checkbox was checked.

Need a hint?

JCheckBox has an isSelected() method that returns a boolean — read it inside the button's listener, not when the checkbox itself changes.

Check the worked solution

Reading isSelected() inside the Submit button's listener, rather than attaching a separate listener to the checkbox, correctly captures the checkbox's state at the moment of submission — exactly the kind of design decision Unit IV's event-driven model requires you to make deliberately.

JCheckBox subscribeBox = new JCheckBox("Subscribe");
JButton submitButton = new JButton("Submit");

submitButton.addActionListener(e ->
    System.out.println("Subscribed: " + subscribeBox.isSelected()));

Quick check

Why does every Swing component like JButton and JLabel extend JComponent?

Select an answer to check your thinking.

Why this lesson exists

Syllabus mapping

Swing – Introduction, limitations of AWT, MVC architecture, components, containers, exploring swing- JFrame and JComponent, JLabel, ImageIcon, JTextField, JButton, JCheckBox, JRadioButton.

Maps to course outcome CO4.