Nearby lessons
121 of 125Java - GUI with AWT and Swing
- What a GUI is and how Java makes windows
- AWT vs Swing — the difference
- Important components: Button, TextField, Label, etc.
- Layout managers
- Event handling — making buttons work
- The modern replacement: JavaFX
GUI with AWT and Swing is a core concept of the Java language. This lesson explains What is GUI?, AWT vs Swing and Your First Window with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
What is GUI?
A GUI (Graphical User Interface) is the part of a program the user sees and clicks — windows, buttons, text boxes, menus. Before GUI, programs were command-line: you typed commands and got text. GUI made computers friendly.
Java has two classic GUI toolkits: AWT (old, uses operating system components) and Swing (newer, draws its own components). Both live in the java.awt and javax.swing packages.
AWT vs Swing
| Point | AWT | Swing |
|---|---|---|
| Full form | Abstract Window Toolkit | Swing (part of Java Foundation Classes) |
| Components | Uses OS components (heavyweight) | Draws its own (lightweight) |
| Look & feel | Depends on the operating system | Same everywhere (pluggable) |
| Speed | Faster (native) | A little slower (Java-drawn) |
| Richness | Few components | Many rich components (JTable, JTree) |
| MVC support | No | Yes |
| Which to use | Legacy only | The classic standard |
Your First Window
That opens a simple resizable window with the title My First Window. setVisible(true) shows it; EXIT_ON_CLOSE makes the app close when you press the X.
Common Components
| Component | What it shows/does |
|---|---|
| JLabel | A short text or picture (non-editable). |
| JTextField | One-line box where the user types text. |
| JPasswordField | Text field that hides the typed characters. |
| JButton | A clickable button. |
| JTextArea | Multi-line text area. |
| JCheckBox | A tick box (choose any number). |
| JRadioButton | A round button (choose only one). |
| JComboBox | A drop-down list. |
| JList | A list of items. |
A Form with Components
setBounds(x, y, width, height) places a component. The numbers are pixels from the top-left corner of the window.
Layout Managers
Instead of manual positioning, layout managers arrange components automatically:
| Layout | How it arranges |
|---|---|
| FlowLayout | Left to right, new row when space ends (default). |
| BorderLayout | Five regions: North, South, East, West, Center. |
| GridLayout | Equal-size grid of rows and columns. |
| GridBagLayout | Most powerful (and complex) flexible grid. |
| null | No manager — you place everything manually. |
Event Handling — Making Buttons Work
A button does nothing until we attach an event listener. The listener is an object that waits for the click and runs our code. This is where the anonymous inner class from Chapter 6 shows up in real life.
Common Event Types
| Event | Listener | When it fires |
|---|---|---|
| Button click / action | ActionListener | When a button is pressed or Enter in a text field |
| Mouse move / click | MouseListener | When the mouse does something |
| Key press | KeyListener | When a key is typed |
| Window close | WindowListener | When the window opens/closes |
| Text change | DocumentListener | When text in a field changes |
The Modern Replacement — JavaFX
CUI vs GUI — Question and Answer
The classic material begins the GUI chapter with this question. CUI = Character User Interface, GUI = Graphical User Interface.
| Point | CUI | GUI |
|---|---|---|
| Full form | Character User Interface | Graphical User Interface |
| Interaction | Typing commands and text | Clicking buttons, windows, menus |
| Examples | Command prompt, old DOS programs | Windows, mobile apps |
| Ease of use | Difficult for beginners | Easy and friendly |
| Looks | Only text | Colourful graphics and images |
Java programs can be CUI (console programs like our earlier chapters) or GUI (AWT/Swing windows). The classic AWT program builds a Frame and adds components like Button and Label:
- GUI = windows and clickable components; AWT uses OS parts, Swing draws its own.
- JFrame is the window; components like JLabel, JTextField, JButton go inside it.
- Layout managers (Flow, Border, Grid) arrange components automatically.
- Events need listeners: addActionListener runs code on a click.
- Anonymous inner classes or lambdas provide the event code.
- Modern GUI work uses JavaFX; Swing still appears in exams and legacy projects.