Introduction
Java provides two major GUI toolkits: the original Abstract Window Toolkit (AWT), which wraps native platform widgets, and Swing, a lightweight toolkit written entirely in Java that draws its own components. Swing is preferred because it looks identical on every platform and provides a much richer set of components. Both toolkits share the same event-delegation model introduced in JDK 1.1.
Containers, Frames and Panels
A container is a component that can hold other components. A JFrame is the top-level window of a Swing application — it has a title bar, menu bar area, and a content pane. A JPanel is a general-purpose container used to group related components inside a frame.
import javax.swing.*;
public class HelloGUI {
public static void main(String[] args) {
JFrame f = new JFrame("Hello");
JPanel p = new JPanel();
p.add(new JLabel("Hello, World!"));
p.add(new JButton("OK"));
f.add(p);
f.setSize(300, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}Layout Managers
A layout manager controls how components inside a container are sized and positioned. Swing ships with several: FlowLayout (default for JPanel, left-to-right flowing), BorderLayout (default for JFrame, five regions: North/South/East/West/Center), GridLayout (rectangular grid of equal-size cells), BoxLayout (single row or column), GridBagLayout (highly flexible grid with variable-size cells), and CardLayout (stack of panels shown one at a time, like tabs).
JPanel grid = new JPanel(new GridLayout(2, 3));
for (int i=1; i<=6; i++) grid.add(new JButton("B"+i));Event Delegation Model
Swing uses the event delegation model: an event source (button, text field) generates an event object when something happens, and delivers it to every listener that has registered interest. The source does not know what the listeners will do — it simply notifies them.
JButton btn = new JButton("Click");
btn.addActionListener(e -> System.out.println("Clicked at " + e.getWhen()));Event Categories and Listeners
Each event type has a matching listener interface:
| Event | Listener | Typical source |
|---|---|---|
| ActionEvent | ActionListener | Button, menu item, text field |
| MouseEvent | MouseListener, MouseMotionListener | Any component |
| KeyEvent | KeyListener | Any component with focus |
| WindowEvent | WindowListener | JFrame, JDialog |
| ItemEvent | ItemListener | Checkbox, combo box |
| FocusEvent | FocusListener | Any component |
Adapter Classes
Listener interfaces with many methods have convenience adapter classes that provide empty default implementations. For example, MouseAdapter implements MouseListener and MouseMotionListener so you can override just the one method you care about.
panel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) { System.out.println("clicked " + e.getPoint()); }
});Common Swing Components
JLabel— non-editable text or iconJButton— clickable buttonJTextField,JPasswordField— single-line text inputJTextArea— multi-line text inputJCheckBox,JRadioButton(withButtonGroup)JComboBox,JList— selection from a listJTable,JTree— tabular and hierarchical dataJMenuBar,JMenu,JMenuItem— menusJDialog,JOptionPane— dialog boxes
Model-View-Controller (MVC)
Swing components follow the MVC pattern. The model holds the data (e.g. DefaultTableModel), the view renders it (e.g. JTable), and the controller handles user input. Components expose their models via getModel() / setModel(), so the same data can be displayed in multiple views, and UI and data can evolve independently.
Threading: EDT
All Swing components must be accessed from the Event Dispatch Thread (EDT). Long-running work should run on a background thread using SwingWorker so the UI stays responsive. Use SwingUtilities.invokeLater() to post GUI updates back to the EDT.
Summary
Swing is Java's preferred GUI toolkit. Build a UI by placing components inside a JFrame, controlling position with layout managers, and responding to user input via listeners. Follow MVC, and keep all Swing calls on the Event Dispatch Thread.
Important Questions
- Differentiate AWT and Swing.
- Explain five common layout managers with use cases.
- What is the event delegation model? Give an example.
- List Swing components for text input, selection, and tabular display.
- What is the MVC pattern and how does Swing use it?
- What is the Event Dispatch Thread and why must GUI updates happen there?
- Write a Swing program with a label, text field, and button that echoes the typed text.
- What are adapter classes and when are they useful?