What is AWT in Java?
AWT = Abstract Window Toolkit
It is a set of classes in Java used to make Graphical User
Interfaces (GUIs).
It was the first GUI library in Java.
AWT provides basic building blocks like:
o Button
o Label
o TextField
o Checkbox
o Frame (window)
o Menus
👉 Example: AWT can create a simple window with a button inside it.
🎨 What is GUI?
GUI = Graphical User Interface.
It is the visual part of a program that users interact with
(instead of typing commands).
Examples in daily life:
o Calculator app → buttons for numbers and operators
o MS Paint → menus, colors, canvas
o Browser → tabs, buttons, input fields
So, GUI = What you see & click
AWT = A Java toolkit to build that GUI
✨ Relationship between AWT and GUI
GUI is a general concept (any app with windows, buttons,
icons, etc.).
AWT is one way in Java to create GUI apps.
Later, Swing and JavaFX came → these are more modern GUI
libraries than AWT.
GUI = The concept of using windows, buttons, menus, etc.
AWT = Java’s old toolkit to make GUI.
Q1
Statement-1: Java AWT (Abstract Window Toolkit) is an API to
develop GUI.
Statement-2: Graphical User Interface (GUI) offers user interaction
via some graphical components.
✅ Answer: Both statements are correct.
Explanation:
AWT is one of the first GUI toolkits in Java. It has components
like Button, Label, Checkbox, TextField, etc. So Statement-1 is
correct.
GUI = graphical user interface → things like buttons, menus,
text fields that you can click, type in, etc. It allows interaction
through graphics instead of typing commands. So Statement-2
is also correct.
What is a Listener?
A listener is like an ear in your program.
It waits and listens for some action (like button click, key
press, mouse movement).
When the user does something → the listener responds by
running some code.
👉 Example:
You click a button → ActionListener hears it → runs your code.
You press a key → KeyListener hears it → runs your code.
Types of Listeners in AWT
Some common ones:
1. ActionListener → listens to button clicks, menu selections
2. ItemListener → listens to checkbox, radio button, choice item
changes
3. KeyListener → listens to keyboard key press/release
4. MouseListener → listens to mouse clicks, enters, exits
5. WindowListener → listens to window open, close, minimize,
etc.
6. Listener = special object that waits for events.
7. It is part of event-driven programming in Java.
8. Without listeners → GUI would not respond to user actions.
Q2
Statement-1: Event driven programming is a natural way of dealing
with GUI interactions.
Statement-2: Listeners implement methods that respond
appropriately to different types of events.
✅ Answer: Both statements are correct.
Explanation:
GUI works on events (button clicked, key pressed, mouse
moved). That’s called event-driven programming.
A listener is an interface in Java that has methods like
actionPerformed, itemStateChanged, etc. We implement these
methods to say what to do when an event happens.
What is an Event in Java GUI?
An event is simply an action done by the user on the GUI.
Example:
o Clicking a button
o Pressing a key
o Moving the mouse
o Closing a window
👉 So:
Event = Action
Listener = Ear (listening for the action)
Handler = Code that runs when the event happens
⚡ Event-Driven Programming (in Java GUI)
AWT uses event-driven programming.
The flow is:
1. User performs an action (event happens).
2. Event object is created (like ActionEvent, MouseEvent).
3. Listener catches it.
4. Listener method executes your code.
🎨 Types of Events in Java AWT
Here are the main ones:
Event
Event Type Triggered when...
Class
ActionEve
Action Event Button click, menu item selected
nt
Checkbox ticked/unticked, list
Item Event ItemEvent
item selected
Key Event KeyEvent Key pressed, released, or typed
MouseEve Mouse clicked, moved, entered,
Mouse Event
nt exited
Mouse Motion MouseEve
Mouse moved or dragged
Event nt
WindowEv Window opened, closed,
Window Event
ent minimized, etc.
Text in a TextField or TextArea
Text Event TextEvent
changes
FocusEven When a component gains or loses
Focus Event
t focus
Event = Action (click, press, move, etc.)
Listener = Object that listens for the event
Event Object = Contains details about the event (like which
button was clicked, which key pressed, etc.)
Handler Method = Code you write to respond
What is Swing in Java?
Swing is a GUI toolkit in Java (like AWT, but more advanced).
It is part of Java Foundation Classes (JFC).
Built on top of AWT, but gives better, modern, flexible GUI
components.
Introduced after AWT because AWT was too basic and
platform-dependent.
👉 Swing components are lightweight (they don’t depend on OS UI) →
so they look the same on all platforms (Windows, Mac, Linux).
🆚 Difference: AWT vs Swing
Feature AWT (Old) Swing (New)
Platform Heavyweight → uses Lightweight → looks same on
dependency OS’s GUI components all systems
Pluggable Look & Feel (can
Look & Feel Native look only
change styles)
Fewer (Button, Label, Rich (JButton, JTable, JTree,
Components
TextField, etc.) JComboBox, JTabbedPane, etc.)
Flexibility Limited More powerful
Package [Link].* [Link].*
🎨 Common Swing Components
JFrame → Main window (instead of Frame)
JButton → Button
JLabel → Text label
JTextField → Text box (single line)
JTextArea → Text box (multi-line)
JCheckBox → Checkbox
JRadioButton → Radio button
JComboBox → Drop-down list
JTable → Table
JTree → Tree structure
Q3
Which among the following are Swing components?
JButton
JCheckBox
JRadioButton
JBox
✅ Answer: JButton, JCheckBox, JRadioButton.
Explanation:
Swing components start with J (JButton, JCheckBox,
JRadioButton, JLabel, JTable etc).
JBox doesn’t exist → instead there is Box (a layout helper
class), not a Swing component.
Q4
Listener interface of JMenuItem and JToggleButton?
✅ Answer: ActionListener.
Explanation:
JMenuItem → when you select a menu item, it fires an
ActionEvent.
JToggleButton (button that can be pressed ON/OFF) also fires
an ActionEvent.
So both use ActionListener.
Q5
Listener interface of JButton?
✅ Answer: ActionListener.
Explanation:
JButton = normal button. When clicked, it generates an
ActionEvent.
To handle this, we attach an ActionListener.
Q6
Match the following
A. ActionListener → III. actionPerformed(ActionEvent e)
B. WindowListener → I. windowOpened(WindowEvent e)
C. KeyListener → IV. keyTyped(KeyEvent e)
D. ItemListener → II. itemStateChanged(ItemEvent e)
✅ Answer: A-III, B-I, C-IV, D-II.
Explanation:
ActionListener → reacts to actions like button click.
WindowListener → reacts when a window opens/closes.
KeyListener → reacts to keyboard keys typed.
ItemListener → reacts when an item in checkbox/radio is
selected.
Q7
Which package contains KeyListener, ItemListener, WindowListener,
ActionListener?
✅ Answer: [Link]
Explanation:
[Link] is the package that has all listener interfaces.
[Link] → has GUI components (Button, Label).
[Link] → has Swing components (JButton, JCheckBox).
Q8
Match the following
A. JButton → (4,5) → addActionListener(listener) +
actionPerformed(EventClass obj)
B. JRadioButton → (1,6) → fires ItemEvent, uses addItemListener()
C. JCheckBox → (1,6) → same as JRadioButton (ItemListener)
D. Keyboard → (2,3) → addKeyListener(listener), methods from
KeyListener
✅ Answer: A-(4,5), B-(1,6), C-(1,6), D-(2,3).
Explanation:
JButton uses ActionListener.
JRadioButton and JCheckBox use ItemListener.
Keyboard events use KeyListener.
So the pattern is:
ActionListener → Buttons, MenuItems.
ItemListener → Radio, Checkbox.
KeyListener → Keyboard.
WindowListener → Window events.
Q1.
Statement-1: One listener can listen to multiple objects. ✅ (True)
Example: Same ActionListener can handle clicks from 2 buttons.
Statement-2: One component can inform multiple listeners. ✅ (True)
Example: One button can have 2 different listeners attached, and
both will get notified.
✔️Correct Answer: Both statements are correct.
Q2.
Steps to display a button properly in Swing:
1. Create the button.
2. Make the panel a listener (not frame directly, usually better
practice).
3. Add the button to the panel.
4. Add the panel to the frame.
✔️Correct Answer:
Create the button, make the panel a listener and add the button to
the panel and add the panel to the frame.
Q3.
Top-level class in Swing hierarchy?
Component → Superclass for GUI objects.
Container → Can hold components.
JPanel → Not top-level (used inside containers).
JFrame → Top-level window class (main window).
✔️Correct Answer: JFrame
Q4.
Class used to create password field in Swing?
JTextField → Normal text box.
JPasswordField → Special field that hides typed characters with
***.
JPasswordTextField / JHiddenField → ❌ not real classes.
✔️Correct Answer: JPasswordField
Q5.
Advantages of Swing over AWT?
Swing look-and-feel is platform independent (same on
Windows, Mac, Linux). ✅
Swing components are light-weight (drawn by Java, not OS). ✅
AWT is heavy-weight (uses OS components).
✔️Correct Answer:
Swing component’s look-and-feel is platform independent.
Swing components are light-weight than AWT components.
Q6.
Classes used to create a window/frame in Swing & AWT?
Panel → Container (not top-level).
Frame → AWT window.
JPanel → Swing container.
JFrame → Swing window.
✔️Correct Answer: All of the above
Q7.
Container classes in Swing & AWT?
Panel ✅
Frame ✅
JPanel ✅
JFrame ✅
✔️Correct Answer: All of the above
What is a Layout in Java GUI?
A Layout Manager in Java controls how components are
arranged inside a window (Frame / JFrame / Panel).
Instead of manually giving setBounds(x, y, w, h) to each
component → Layouts automatically position them.
This makes GUI resize-friendly and clean.
🎨 Types of Layout Managers
Here are the most common ones:
1. FlowLayout
Default for Panel.
Arranges components in a row, left to right.
If space runs out → moves to next line.
👉 Example:
setLayout(new FlowLayout());
📌 Looks like how words flow in a paragraph.
2. BorderLayout
Default for Frame.
Divides container into 5 regions:
o NORTH (top)
o SOUTH (bottom)
o EAST (right)
o WEST (left)
o CENTER (middle)
👉 Example:
setLayout(new BorderLayout());
add(new Button("North"), [Link]);
add(new Button("South"), [Link]);
add(new Button("East"), [Link]);
add(new Button("West"), [Link]);
add(new Button("Center"), [Link]);
📌 Useful for toolbars, menus, main content areas.
3. GridLayout
Divides container into a grid of rows and columns.
All cells are equal size.
👉 Example:
setLayout(new GridLayout(2, 3)); // 2 rows, 3 columns
add(new Button("1"));
add(new Button("2"));
add(new Button("3"));
add(new Button("4"));
add(new Button("5"));
add(new Button("6"));
📌 Looks like a table.
4. CardLayout
Think of it like stack of cards.
Only one card (panel) is visible at a time.
You can flip between them.
👉 Example: Wizard screens, login/register screens.
5. GridBagLayout (Advanced)
Most powerful but complex.
Allows flexible, uneven grid with components of different sizes.
You specify constraints (like position, span, alignment).
📌 Used for professional-looking forms.
FlowLayout → Left to right (like text flow).
BorderLayout → Divides into North, South, East, West, Center.
GridLayout → Equal cells (rows × cols).
CardLayout → One card visible at a time.
GridBagLayout → Advanced flexible grid.
GridLayout — like a chessboard
Places components into equal-sized cells in rows × columns.
Use when you want a neat grid (buttons, cells).
BorderLayout — like a compass (N, S, E, W, Center)
Has five regions: NORTH, SOUTH, EAST, WEST, CENTER.
Good for top/bottom menus, side panels + central area.
FlowLayout — like text flowing in a paragraph
Adds components left → right; when no space, wraps to next
line.
Default for JPanel.
Defaults — why they matter
JFrame’s content pane default = BorderLayout.
So [Link](comp) without constraints will place comp in
CENTER (or sometimes behave as BorderLayout CENTER).
JPanel default = FlowLayout.
New panels by default lay out their children left-to-right and
wrap.
(“FrameLayout” is not a standard Java layout — ignore that option.)
Two-dimensional grid layout → GridLayout
South/East/West/North/Center layout → BorderLayout
Left-to-right, top-to-bottom (wrap) → FlowLayout
Default layout manager for JFrame → BorderLayout
Default layout manager for JPanel → FlowLayout