Mastering Java's Abstract Window Toolkit (java.awt.*) for exam coding questions. Learn component instantiation, layout nesting, event handling, and bulletproof boilerplate templates.
In Java AWT, everything inherits from java.awt.Component (or java.awt.Container). Here is the master lookup table of core components frequently required in exam questions:
| Component | Java Class | Exam Use Case & Key Methods |
|---|---|---|
| Window Container | Frame |
Top-level window. Default layout: BorderLayout. setSize(w, h), setLayout(), setVisible(true) |
| Sub-Container | Panel |
Organizes sub-groups of components. Default layout: FlowLayout. panel.add(comp) |
| Static Text | Label |
Displays non-editable text labels. new Label("Username:"), setText(str) |
| Single-Line Input | TextField |
Input box for text or passwords. getText(), setText(), setEchoChar('*') (for password) |
| Multi-Line Text | TextArea |
Multi-line scrollable text block. append(str), getText() |
| Push Button | Button |
Clickable button. Triggers ActionEvent. addActionListener(...) |
| Checkbox / Radio | Checkbox |
Toggle option. Use with CheckboxGroup for Radio Buttons! getState() returns boolean. |
| Dropdown Choice | Choice |
Single-selection dropdown list. add("Option 1"), getSelectedItem() |
| List Box | List |
Scrollable item list. add("Item"), getSelectedItems() |
JRadioButton class! In AWT, you create radio buttons by assigning multiple Checkbox objects to a single CheckboxGroup:
AWT Layout Managers handle positioning automatically across platforms. Do not rely on hardcoded coordinates (`setLayout(null)`) unless specifically requested!
BorderLayout: Divides space into 5 regions (NORTH, SOUTH, EAST, WEST, CENTER). Components expand to fill their region. Default for Frame.FlowLayout: Places components side-by-side left-to-right, wrapping to the next line. Default for Panel.GridLayout(rows, cols, hgap, vgap): Divides container into equal rectangular cells. Ideal for forms and calculators!BorderLayout on the main Frame, put input fields in a GridLayout panel in CENTER, and put buttons in a FlowLayout panel in SOUTH!AWT uses the Delegation Event Model. An Event Source (e.g. Button) generates an Event Object (e.g. ActionEvent) and notifies registered Event Listeners.
In AWT, clicking the window close button ('X') does NOT close the application automatically. You must register a WindowListener or WindowAdapter:
Memorize this clean, bulletproof structure to write any AWT GUI exam question in under 10 minutes:
| # | Examiner Trap | Why It Fails | Distinction Solution |
|---|---|---|---|
| 1 | Clicking 'X' does not close window | AWT Frame has no default close operation like Swing's EXIT_ON_CLOSE. |
Must add addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); |
| 2 | Window appears completely blank | Forgot setVisible(true) or called it before adding components. |
Always call setVisible(true) at the very end of constructor after all components are added. |
| 3 | Components overlap or take up entire window | Adding multiple components to BorderLayout region without specifying location, or defaulting to CENTER. |
Use nested Panel objects with GridLayout or FlowLayout inside BorderLayout regions. |
| 4 | Creating Radio Buttons using `JRadioButton` in AWT | `JRadioButton` belongs to Swing (`javax.swing.*`), not AWT! | Use Checkbox associated with a CheckboxGroup. |
| 5 | Password field shows plain text | Using standard TextField without setting echo character. |
Call pwdField.setEchoChar('*'); on the TextField. |
| 6 | Password read comparison fails | Calling getText() on password field without trimming whitespace. |
Use txtPwd.getText().trim(). |
Q: Explain why WindowAdapter is preferred over implementing WindowListener directly in an AWT Frame class.
WindowListener is an interface containing 7 abstract methods (windowOpened, windowClosing, windowClosed, windowIconified, windowDeiconified, windowActivated, windowDeactivated). If you implement the interface directly, you are forced to override all 7 methods, leading to redundant empty method bodies.WindowAdapter is an abstract adapter class that provides empty default implementations for all 7 methods. Extending WindowAdapter allows you to override only the method you need (e.g. windowClosing), keeping exam code concise and error-free.