unit 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

UNIT 4 NOTES

1. What is the AWT Event Hierarchy?

 The AWT Event Hierarchy defines the structure of events in the Abstract Window
Toolkit.
 It includes classes for event types like ActionEvent, MouseEvent, and KeyEvent.
 Events are generated by user interactions with GUI components.
 Each event has a source (the component that generated it) and associated data.

2. What are AWT Components?

 AWT components are the building blocks of a graphical user interface in Java.
 Examples include buttons, labels, text fields, and checkboxes.
 Components are part of the AWT package and extend the Component class.
 They are used to create interactive applications and can be added to containers.

3. What is Graphics Programming in AWT?

 Graphics programming involves drawing shapes, text, and images on a GUI.


 It is done using the Graphics class, which provides methods for drawing.
 Common methods include drawLine(), drawRect(), and drawString().
 AWT provides support for basic 2D graphics and rendering.

4. What is an Applet?

 An applet is a small Java program that runs in a web browser or applet viewer.
 It extends the Applet class and is typically used for web-based applications.
 Applets have lifecycle methods like init(), start(), stop(), and destroy().
 They can interact with the user and display graphics.

5. What is a Frame in AWT?

 A Frame is a top-level window with a title and a border.


 It is created using the Frame class, which is part of the AWT package.
 A Frame can contain multiple components and supports various layout managers.
 It serves as the main window for AWT applications.

6. Define Dialog boxes.

User Interaction: Dialog boxes in AWT are used to facilitate interaction with
the user, typically for displaying messages, receiving input, or confirming actions. They
provide a way to gather information or notify the user.
Types: AWT provides two types of dialog boxes—Modal (blocks user
interaction with other windows until closed) and Modeless (allows interaction with other
windows even when the dialog is open).
Common Use Cases: Dialog boxes are used for tasks such as displaying
alerts, warnings, or error messages, prompting for confirmation (e.g., "OK" or "Cancel"),
or inputting data.
Classes: In AWT, the Dialog class is used to create dialog boxes, often
attached to a parent Frame, and it can be customized to suit various needs (e.g., input
dialogs, message dialogs).

7. How do you work with 2D Shapes in AWT?


 You can create and manipulate 2D shapes using the Graphics2D class.
 The Graphics2D class provides methods like draw(), fill(), and setStroke().
 You can create shapes such as rectangles, circles, and polygons.
 2D graphics enhance visual representation in applications.

8. How do you use Color, Fonts, and Images in AWT?

 Colors are defined using the Color class and can be applied to shapes and text.
 Fonts are managed with the Font class, allowing customization of text appearance.
 Images can be loaded using the Image class and displayed on components.
 The Graphics class provides methods to draw colors, fonts, and images.

9. Define event sources in AWT.

Event sources in Java AWT (Abstract Window Toolkit) are the components or objects
that generate events in response to user actions. When a user interacts with a GUI
element—such as clicking a button, typing in a text field, or moving the mouse—an event
source creates an event object that encapsulates information about that interaction.

Event sources are responsible for:

 Generating Events: They produce events when user actions occur (e.g., a button
click generates an ActionEvent).
 Notifying Listeners: Event sources maintain a list of registered event listeners
that are notified when an event occurs, allowing the application to respond appropriately.
 Examples: Common event sources include GUI components like buttons,
checkboxes, text fields, menus, and windows.

10. What is event handling in AWT?

 Event handling is the process of responding to user actions (events) in GUI


applications.
 It involves creating event listeners to listen for specific events.
 Listeners are interfaces that define methods for handling events, such as
actionPerformed().
 Proper event handling ensures that applications respond correctly to user input.

11. What are needs of Mouse Events in AWT/Swing?

1. User Interaction: Mouse events facilitate user interaction with graphical user interfaces
(GUIs) by responding to actions like clicking, dragging, and releasing the mouse.
Common mouse events include MouseClicked, MousePressed, MouseReleased,
MouseEntered, and MouseExited.
2. Event Handling: They enable developers to implement specific behaviors in response to
mouse actions. For example, clicking a button can trigger an action event, and dragging
can move components. Mouse event handling is done by registering mouse event
listeners, allowing the application to respond appropriately to user actions.

12. What are Adapter Classes in AWT?

 Adapter classes are abstract classes that implement event listener interfaces with
default methods.
 They allow developers to override only the methods they need, reducing boilerplate
code.
 Common adapter classes include MouseAdapter and KeyAdapter.
 They simplify the implementation of event handling in AWT applications.

13. What is Swing in Java?

 Swing is a part of the Java Foundation Classes (JFC) used for building graphical
user interfaces.
 It provides a richer set of GUI components compared to AWT, such as JTable,
JTree, and JTabbedPane.
 Swing components are lightweight and more customizable than AWT components.
 It also supports advanced features like pluggable look-and-feel, which allows
changing the appearance of the UI.

PART

I. AWT with Swing

II. Event hierarchy in AWT

 Event Class: The root of the event hierarchy. All event types are subclasses of the
java.awt.AWTEvent class.

 Key Events: Triggered by keyboard actions. Subclass: KeyEvent

 Examples: KEY_PRESSED, KEY_RELEASED, KEY_TYPED.

 Mouse Events: Triggered by mouse actions. Subclass: MouseEvent


 Examples: MOUSE_CLICKED, MOUSE_PRESSED, MOUSE_RELEASED, MOUSE_MOVED,
MOUSE_DRAGGED.

 Component Events: Related to changes in component state. Subclass: ComponentEvent

 Examples: COMPONENT_RESIZED, COMPONENT_MOVED, COMPONENT_SHOWN,


COMPONENT_HIDDEN.

 Window Events: Related to window actions. Subclass: WindowEvent

 Examples: WINDOW_OPENED, WINDOW_CLOSED, WINDOW_ACTIVATED,


WINDOW_DEACTIVATED.

 Action Events: Triggered by button presses or menu selections. Subclass: ActionEvent

 Example: ACTION_PERFORMED.
 Example Program

import java.awt.*;

import java.awt.event.*;

public class EventExample extends Frame implements KeyListener, MouseListener,


WindowListener {

Label label;

public EventExample() {

// Frame setup

setSize(400, 400);

setTitle("AWT Event Handling Example");

setLayout(new FlowLayout());

label = new Label("Interact with the frame!");

add(label);

// Add listeners

addKeyListener(this);

addMouseListener(this);

addWindowListener(this);

// Close the window

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {

System.exit(0);
}

});

setVisible(true);

// KeyListener methods

public void keyPressed(KeyEvent e) {

label.setText("Key Pressed: " + KeyEvent.getKeyText(e.getKeyCode()));

public void keyReleased(KeyEvent e) {

label.setText("Key Released: " + KeyEvent.getKeyText(e.getKeyCode()));

public void keyTyped(KeyEvent e) {

// Not used

// MouseListener methods

public void mouseClicked(MouseEvent e) {

label.setText("Mouse Clicked at (" + e.getX() + ", " + e.getY() + ")");

public void mousePressed(MouseEvent e) {

// Not used

public void mouseReleased(MouseEvent e) {

// Not used

public void mouseEntered(MouseEvent e) {

label.setText("Mouse Entered Frame");

public void mouseExited(MouseEvent e) {


label.setText("Mouse Exited Frame");

// WindowListener methods

public void windowOpened(WindowEvent we) { }

public void windowClosing(WindowEvent we) { }

public void windowClosed(WindowEvent we) { }

public void windowIconified(WindowEvent we) { }

public void windowDeiconified(WindowEvent we) { }

public void windowActivated(WindowEvent we) { }

public void windowDeactivated(WindowEvent we) { }

public static void main(String[] args) {

new EventExample();

Explanation of the Program

1. Frame Setup: A Frame is created, and its layout is set to FlowLayout. A label is added
to display event messages.
2. Event Listeners:
o The program implements KeyListener, MouseListener, and WindowListener to
respond to keyboard, mouse, and window events, respectively.
o addKeyListener, addMouseListener, and addWindowListener are used to attach
these listeners to the frame.
3. Event Handling Methods:
o Key Events: The keyPressed and keyReleased methods update the label with the
corresponding key information.
o Mouse Events: The mouseClicked method captures mouse click positions, while
mouseEntered and mouseExited provide feedback when the mouse enters or exits
the frame.
o Window Events: The windowClosing method ensures the application closes when the
window is closed.

III. Mouse events in AWT

Mouse events in AWT are primarily handled through the MouseListener and
MouseMotionListener interfaces. The main mouse events include:

1. Mouse Clicked: Triggered when the mouse button is clicked (pressed and released).
2. Mouse Pressed: Triggered when the mouse button is pressed down.
3. Mouse Released: Triggered when the mouse button is released.
4. Mouse Entered: Triggered when the mouse cursor enters the bounds of a component.
5. Mouse Exited: Triggered when the mouse cursor exits the bounds of a component.
6. Mouse Moved: Triggered when the mouse is moved within the component.
7. Mouse Dragged: Triggered when the mouse is moved while a button is pressed.

Program:

import java.awt.*;

import java.awt.event.*;

public class MouseEventExample extends Frame implements MouseListener,


MouseMotionListener {

Label label;

public MouseEventExample() {

// Frame setup

setSize(400, 400);

setTitle("Mouse Event Handling Example");

setLayout(new FlowLayout());

label = new Label("Mouse Event Demo");

add(label);

// Add mouse listeners

addMouseListener(this);

addMouseMotionListener(this);

// Close the window

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {

System.exit(0);

});

setVisible(true);

// MouseListener methods

public void mouseClicked(MouseEvent e) {


label.setText("Mouse Clicked at (" + e.getX() + ", " + e.getY() + ")");

public void mousePressed(MouseEvent e) {

label.setText("Mouse Pressed at (" + e.getX() + ", " + e.getY() + ")");

public void mouseReleased(MouseEvent e) {

label.setText("Mouse Released at (" + e.getX() + ", " + e.getY() + ")");

public void mouseEntered(MouseEvent e) {

label.setText("Mouse Entered Frame");

public void mouseExited(MouseEvent e) {

label.setText("Mouse Exited Frame");

// MouseMotionListener methods

public void mouseDragged(MouseEvent e) {

label.setText("Mouse Dragged at (" + e.getX() + ", " + e.getY() + ")");

public void mouseMoved(MouseEvent e) {

label.setText("Mouse Moved at (" + e.getX() + ", " + e.getY() + ")");

public static void main(String[] args) {

new MouseEventExample();

Explanation of the Program

1. Frame Setup:
o A Frame is created with a size of 400x400 pixels and a title.
o A Label is added to the frame to display the mouse event information.
2. Adding Listeners:
o The class implements MouseListener and MouseMotionListener, allowing it to
respond to various mouse actions.
o The addMouseListener(this) and addMouseMotionListener(this) methods
attach the listeners to the frame.
3. Event Handling Methods:
o MouseListener Methods:
 mouseClicked(MouseEvent e): Displays the coordinates where the
mouse was clicked.
 mousePressed(MouseEvent e): Shows the coordinates when the mouse
button is pressed.
 mouseReleased(MouseEvent e): Shows the coordinates when the mouse
button is released.
 mouseEntered(MouseEvent e): Updates the label when the mouse enters
the frame.
 mouseExited(MouseEvent e): Updates the label when the mouse exits
the frame.
o MouseMotionListener Methods:
 mouseDragged(MouseEvent e): Updates the label with the coordinates
when the mouse is dragged.
 mouseMoved(MouseEvent e): Updates the label with the coordinates
when the mouse is simply moved.
4. Closing the Window:
o A WindowListener is added to handle closing the window when the user attempts
to close it.

IV. Keyboard events in AWT

Keyboard events in AWT are primarily managed through the KeyListener interface. The main
keyboard events include:

1. Key Pressed: Triggered when a key is pressed down.


2. Key Released: Triggered when a key is released.
3. Key Typed: Triggered when a key is pressed and then released (often used for character
input).

Program:

import java.awt.*;

import java.awt.event.*;

public class KeyEventExample extends Frame implements KeyListener {

Label label;

public KeyEventExample() {

// Frame setup

setSize(400, 200);

setTitle("Keyboard Event Handling Example");


setLayout(new FlowLayout());

label = new Label("Type something...");

add(label);

// Add key listener

addKeyListener(this);

// Close the window

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {

System.exit(0);

});

setVisible(true);

// KeyListener methods

public void keyPressed(KeyEvent e) {

label.setText("Key Pressed: " + KeyEvent.getKeyText(e.getKeyCode()));

public void keyReleased(KeyEvent e) {

label.setText("Key Released: " + KeyEvent.getKeyText(e.getKeyCode()));

public void keyTyped(KeyEvent e) {

label.setText("Key Typed: " + e.getKeyChar());

public static void main(String[] args) {

new KeyEventExample();

}
Explanation of the Program

1. Frame Setup:
o A Frame is created with a size of 400x200 pixels and a title.
o A Label is added to the frame to display keyboard event messages.
2. Adding Key Listener:
o The class implements the KeyListener interface, allowing it to respond to
keyboard events.
o The addKeyListener(this) method attaches the listener to the frame.
3. Event Handling Methods:
o KeyListener Methods:
 keyPressed(KeyEvent e): This method is invoked when a key is pressed
down. It updates the label with the name of the key pressed using
KeyEvent.getKeyText(e.getKeyCode()).
 keyReleased(KeyEvent e): This method is called when the key is
released. It updates the label similarly to show which key was released.
 keyTyped(KeyEvent e): This method is triggered when a character key is
typed (pressed and released). It updates the label to display the character
corresponding to the key typed.
4. Closing the Window:
o A WindowListener is added to handle closing the window when the user attempts
to close it

V. Button source event

The key event types associated with buttons are primarily related to actions triggered by the user,
such as clicking a button. The main event type is:

 Action Event: This is triggered when a button is clicked.

Program:

import java.awt.*;

import java.awt.event.*;

public class ButtonEventExample extends Frame implements ActionListener {

Label label;

Button button;

public ButtonEventExample() {

// Frame setup

setSize(300, 150);

setTitle("Button Event Handling Example");

setLayout(new FlowLayout());

label = new Label("Press the button");

add(label);
button = new Button("Click Me");

add(button);

// Add action listener to the button

button.addActionListener(this);

// Close the window

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {

System.exit(0);

});

setVisible(true);

// ActionListener method

public void actionPerformed(ActionEvent e) {

label.setText("Button Clicked!");

public static void main(String[] args) {

new ButtonEventExample();

Explanation of the Program


1. Frame Setup:
o A Frame is created with a size of 300x150 pixels and a title.
o A Label is added to display instructions and feedback.
o A Button labeled "Click Me" is created and added to the frame.
2. Adding Action Listener:
o The class implements the ActionListener interface, enabling it to respond to
action events from the button.
o The button’s addActionListener(this) method is called to attach the listener to
the button.
3. Event Handling Method:
o actionPerformed(ActionEvent e): This method is called when the button is
clicked. It updates the label to show "Button Clicked!" when the event is
triggered.
4. Closing the Window:
o A WindowListener is added to handle closing the window when the user attempts
to close it.

You might also like