0% found this document useful (0 votes)
8 views9 pages

AWT and Swing GUI Concepts Explained

The document provides a comprehensive overview of the Abstract Window Toolkit (AWT) and Swing in Java, including their hierarchies, differences, layout managers, and event handling models. It explains the Model-View-Controller (MVC) architecture, features of Swing, and how to create menus and components in both AWT and Swing. Additionally, it discusses the use of anonymous inner classes for event handling.

Uploaded by

Red Gear
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

AWT and Swing GUI Concepts Explained

The document provides a comprehensive overview of the Abstract Window Toolkit (AWT) and Swing in Java, including their hierarchies, differences, layout managers, and event handling models. It explains the Model-View-Controller (MVC) architecture, features of Swing, and how to create menus and components in both AWT and Swing. Additionally, it discusses the use of anonymous inner classes for event handling.

Uploaded by

Red Gear
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

5Th Chapter

1. With the help of diagram describe AWT hierarchy.

AWT (Abstract Window Toolkit) provides classes for building GUI in Java.
All AWT classes belong to a well-defined hierarchy.

AWT Hierarchy Diagram (Text Format)

[Link]

[Link]

-----------------------------------------------------

| | | | |

Container Button Label List TextComponent

| | |

| Scrollbar TextField TextArea

Panel ----- Window

-----------------

| |

Frame Dialog

Explanation

• Component: Base class for all GUI elements.

• Container: Can hold or contain other components.

• Panel: Simple container.

• Window: Top-level container (no title bar).

• Frame: Main application window with title bar.

• Dialog: Popup window.

• Button, Label, List, TextField extend Component.


This hierarchy shows how GUI elements are structured in AWT.

2. Differentiate between AWT and Swing.

AWT Swing

Uses native OS components Pure Java (lightweight)

Slower and platform-dependent Faster and platform-independent

Limited set of components Rich set of advanced components

Looks different on every OS Same look everywhere

No support for pluggable look & feel Supports pluggable look & feel

Components like Button, Label Components like JButton, JLabel, JTable

Swing is more modern, flexible, and feature-rich compared to AWT.

3. What is layout? List types of layout managers in AWT.

A layout manager automatically arranges components within a container.


It removes the need to manually set positions.

Types of Layout Managers in AWT

1. FlowLayout

2. BorderLayout

3. GridLayout

4. CardLayout

5. GridBagLayout

6. Null Layout (Manual layout)

Layout managers help design simple and dynamic GUI.

4. With example explain the following layout managers: FlowLayout, BorderLayout,


CardLayout, GridLayout.

(i) FlowLayout

• Default layout of Panel.


• Places components left to right in a row.

Frame f = new Frame();

[Link](new FlowLayout());

[Link](new Button("A"));

[Link](new Button("B"));

(ii) BorderLayout

• Divides window into: North, South, East, West, Center.

[Link](new BorderLayout());

[Link](new Button("Top"), [Link]);

(iii) CardLayout

• Stacks components like cards; one visible at a time.

Panel p = new Panel();

[Link](new CardLayout());

(iv) GridLayout

• Divides container into equal-sized rows and columns.

[Link](new GridLayout(2,2));

5. Explain MVC architecture diagrammatically.

MVC stands for Model–View–Controller, a GUI architecture.

Diagram

+------------+

| Model |

+------------+

+---------+---------+

| |

+-----------+ +--------------+
| View |<--->| Controller |

+-----------+ +--------------+

Explanation

• Model: Manages data and logic.

• View: Displays output to user.

• Controller: Takes user input and updates model/view.

Swing loosely follows MVC architecture.

6. Define event, source, listeners. Explain Delegation Event Model.

Event

An event is an action generated by the user (click, key press, mouse move).

Source

The component that generates the event. (Example: Button)

Listener

Object that receives the event and handles it.

Delegation Event Model

• Introduced in Java 1.1

• Two objects: event source and event listener

• Source delegates event handling to listener

• Listener implements an event interface (like ActionListener)

This model makes event-driven programming simple and efficient.

7. Which Swing classes are used to create menu?

Swing provides the following classes:

• JMenuBar → Main menu bar

• JMenu → Menu (File, Edit, etc.)

• JMenuItem → Menu items (Open, Save)

• JCheckBoxMenuItem → Checkbox menu item


• JRadioButtonMenuItem → Radio button item

• JSeparator → Horizontal line in menu

These help build modern menus in Swing.

8. Write a short note on: Adapters.

Adapter classes provide empty implementations of listener interfaces.


They help avoid writing all methods of the interface.

Example: MouseAdapter, WindowAdapter, KeyAdapter.

Usage

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

[Link](0);

});

Adapters reduce coding effort.

9. What are the various sources of event in AWT and Swing?

AWT Sources

• Button

• TextField

• TextArea

• List

• Checkbox

• Window

• Mouse & Keyboard devices

Swing Sources

• JButton

• JTextField
• JTextArea

• JCheckBox

• JComboBox

• JTable

• JSlider

• JFrame / JDialog

Any component that generates an event is an event source.

10. Explain in brief the fundamental idea behind MVC architecture.

The main idea:

• Separate data (Model) from

• User interface (View) and

• User interaction handler (Controller).

Why MVC?

• Easy to maintain

• Multiple views of same data

• Logical separation of code

• Better reusability

Swing components internally use a variation of MVC.

11. What are the features of Swing?

1. Pure Java (lightweight components)

2. Platform independent

3. Pluggable look & feel

4. Rich set of components (JTable, JTree, Tooltips)

5. Highly customizable

6. MVC-based

7. Double-buffering for smooth graphics


8. Enhanced event handling

Swing is superior to AWT for modern GUI development.

12. How to create menus in AWT? Explain popup menus with example.

Creating Menu in AWT

Frame f = new Frame();

MenuBar mb = new MenuBar();

Menu m = new Menu("File");

MenuItem i1 = new MenuItem("Open");

[Link](i1);

[Link](m);

[Link](mb);

Popup Menu Example

PopupMenu pm = new PopupMenu();

MenuItem cut = new MenuItem("Cut");

[Link](cut);

[Link](pm);

Popup menus appear on right-click.

13. AWT components: Button, Checkbox, TextArea, ScrollBar

(i) Button

Used to trigger action.

Button b = new Button("Click");

(ii) Checkbox

Multiple selection allowed.

Checkbox cb = new Checkbox("Accept");


(iii) TextArea

Multi-line text input.

TextArea ta = new TextArea(5, 20);

(iv) ScrollBar

Adds scrolling control.

ScrollBar sb = new ScrollBar();

14. Swing Components: JButton, JFileChooser, JRadioButton, ColorChooser

(i) JButton

JButton b = new JButton("OK");

(ii) JFileChooser

Used for selecting files.

JFileChooser fc = new JFileChooser();

(iii) JRadioButton

Single selection within a group.

JRadioButton rb = new JRadioButton("Male");

(iv) JColorChooser

Select color from palette.

Color c = [Link](null, "Pick Color", [Link]);

15. Describe anonymous inner class in detail.

An anonymous inner class is a class with:

• No name

• Defined inside another class

• Used when a class is needed only once

Commonly used for event handling.

Example:

[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {

[Link]("Clicked");

});

Features

• Short and simple

• Eliminates separate class files

• Used with interfaces and abstract classes

You might also like