0% found this document useful (0 votes)
2 views8 pages

Unit 4 Java

The Delegation Event Model in Java allows event sources to delegate event handling to listeners, improving modularity and code reusability in GUI applications. Keyboard event handling is implemented using the KeyListener interface, which detects key actions, while adapter classes simplify event handling by providing default implementations of listener methods. Swing components are preferred over AWT for their lightweight nature, platform independence, and richer set of customizable GUI components.

Uploaded by

silentkillerrblx
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)
2 views8 pages

Unit 4 Java

The Delegation Event Model in Java allows event sources to delegate event handling to listeners, improving modularity and code reusability in GUI applications. Keyboard event handling is implemented using the KeyListener interface, which detects key actions, while adapter classes simplify event handling by providing default implementations of listener methods. Swing components are preferred over AWT for their lightweight nature, platform independence, and richer set of customizable GUI components.

Uploaded by

silentkillerrblx
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

Explain the Delegation Event Model and Event Handling

in Java

Introduction
The Delegation Event Model (DEM) is the standard event handling mechanism in Java. It
allows an object (event source) to delegate the responsibility of handling an event to another
object called the listener. This model provides a simple and efficient way to handle user
interactions in GUI applications.

Components of Delegation Event Model


●​ Event Source: Generates the event (e.g., Button, TextField).
●​ Event Object: Contains information about the event (e.g., ActionEvent).
●​ Event Listener: Receives and processes the event (e.g., ActionListener).

Working
1.​ User performs an action.
2.​ Event source creates an event object.
3.​ The event is sent to the registered listener.
4.​ The listener executes the appropriate method.

Java Program
Output
Button Clicked

Explanation
●​ A button acts as the event source.
●​ ActionListener is registered using addActionListener().
●​ When the button is clicked, the actionPerformed() method is automatically
executed.

Advantages
●​ Separates event generation from event handling.
●​ Improves code reusability.
●​ Makes GUI applications modular and easy to maintain.
●​ Supports multiple event listeners.
2)Discuss keyboard event handling with examples

Introduction
Keyboard event handling in Java is used to detect and respond to keyboard actions such
as pressing, releasing, or typing a key. It is implemented using the KeyListener interface
available in the [Link] package.

Methods of KeyListener
●​ keyPressed(KeyEvent e) – Invoked when a key is pressed.
●​ keyReleased(KeyEvent e) – Invoked when a key is released.
●​ keyTyped(KeyEvent e) – Invoked when a key is typed.

Java Program

Sample Output
Key Pressed
Key Typed
Key Released
Explanation
●​ The class implements the KeyListener interface.
●​ addKeyListener(this) registers the listener.
●​ When a key is pressed, typed, or released, the corresponding method is
automatically executed.

Advantages
●​ Detects keyboard input efficiently.
●​ Useful for games, editors, and GUI applications.
●​ Improves user interaction.
3)Explain about the adapter class with an example

Introduction
An Adapter Class in Java is a class that provides empty implementations of listener
interface methods. It allows the programmer to override only the required methods instead of
implementing all the methods of the listener interface.

Common adapter classes include:

●​ MouseAdapter
●​ KeyAdapter
●​ WindowAdapter

Java Program

Output
A window is displayed and closes when the close button is clicked.
Explanation
●​ WindowAdapter provides default implementations of WindowListener methods.
●​ Only the windowClosing() method is overridden.
●​ When the user clicks the close button, the application terminates.

Advantages
●​ Reduces code complexity.
●​ No need to implement all listener methods.
●​ Improves readability and maintainability.
●​ Makes event handling simpler.
4)Why are Swing components preferred over AWT
components?
(JNTUH R22 Semester Exam Answer)

Introduction
Swing is a part of the Java Foundation Classes (JFC) used to develop graphical user
interface (GUI) applications. It is preferred over AWT (Abstract Window Toolkit) because it
provides a richer set of components, a better look and feel, and platform-independent
behavior.

Advantages of Swing over AWT


●​ Swing components are lightweight, whereas AWT components are heavyweight.
●​ Swing provides a pluggable look and feel, making applications look similar on all
platforms.
●​ Swing offers more GUI components such as JTable, JTree, JTabbedPane, etc.
●​ Swing follows the MVC (Model-View-Controller) architecture, making applications
flexible and easy to maintain.
●​ Swing provides better customization and advanced features.

Comparison Table
AWT Swing

Heavyweight components Lightweight components

Platform dependent Platform independent

Limited GUI components Rich set of GUI components

Less customizable Highly customizable


Java Program

Output
A window titled "Swing Example" is displayed with a "Click" button.

Explanation
●​ JFrame creates the main window.
●​ JButton creates a button and is added to the frame.
●​ Swing components provide a modern and flexible GUI compared to AWT.

You might also like