Event handleing in java., Slides of Java Programming

The two event handling mechanisms in Java, the delegation event model, event sources, event listeners, and event listener interfaces. It also provides examples of programs using key event handlers, mouse event handlers, Java adapter classes, and Java inner classes. useful for students learning Java programming and event handling mechanisms.

Typology: Slides

2021/2022

Available from 01/17/2022

desu-harshith
desu-harshith 🇮🇳

3 documents

1 / 39

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Two Event Handling Mechanisms:
The way in which events are handled changed significantly between
the original version of Java (1.0) and modern versions of Java,
beginning with version 1.1.
The 1.0 method of event handling is still supported, but it is not
recommended for new programs. Also, many of the methods that
support the old 1.0 event model have been deprecated.
The modern approach is the way that events should be handled by all
new programs and thus is the method employed by programs in this
book.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27

Partial preview of the text

Download Event handleing in java. and more Slides Java Programming in PDF only on Docsity!

Two Event Handling Mechanisms:

  • (^) The way in which events are handled changed significantly between the original version of Java (1.0) and modern versions of Java, beginning with version 1.1.
  • (^) The 1.0 method of event handling is still supported, but it is not recommended for new programs. Also, many of the methods that support the old 1.0 event model have been deprecated.
  • (^) The modern approach is the way that events should be handled by all new programs and thus is the method employed by programs in this book.

The Delegation Event Model:

  • (^) The modern approach to handling events is based on the delegation event model, which defines standard and consistent mechanisms to generate and process events.
  • (^) A source generates an event and sends it to one or more listeners.
  • (^) In this scheme, the listener simply waits until it receives an event.
  • (^) Once an event is received, the listener processes the event and then returns.
  • (^) In the delegation event model, listeners must register with a source in order to receive an event notification

Java Event classes and Listener

interfaces :

Event Classes Listener Interfaces ActionEvent ActionListener MouseEvent MouseListener and MouseMotionListener MouseWheelEvent MouseWheelListener KeyEvent KeyListener ItemEvent ItemListener TextEvent TextListener AdjustmentEvent AdjustmentListener WindowEvent WindowListener ComponentEvent ComponentListener ContainerEvent ContainerListener FocusEvent FocusListener

Event Sources :

  • (^) A source is an object that generates an event.
  • (^) Sources may generate more than one type of event.
  • (^) A source must register listeners in order for the listeners to receive notifications about a specific type of event.
  • (^) Each type of event has its own registration method. Here is the general form: public void addTypeListener(TypeListener el) Here, Type is the name of the event, and el is a reference to the event listener. For example, the method that registers a keyboard event listener is called addKeyListener( ).

Event Listeners :

  • (^) A listener is an object that is notified when an event occurs.
  • (^) It has two major requirements.
  • (^) First, it must have been registered with one or more sources to receive notifications about specific types of events.
  • (^) Second, it must implement methods to receive and process these notifications

Event Classes:Main Event Classes in java.awt.event

ActionEvent : Generated when a button is pressed, a list item is double-clicked, or a menu item is selected. AdjustmentEvent: Generated when a scroll bar is manipulated. ComponentEvent: Generated when a component is hidden, moved, resized, or becomes visible. ContainerEvent : Generated when a component is added to or removed from a container. FocusEvent : Generated when a component gains or loses keyboard focus. InputEvent : Abstract superclass for all component input event classes. ItemEvent : Generated when a check box or list item is clicked; also occurs when a choice select is made or a checkable menu item is selected or deselected.

Event Listener Interfaces:

  • (^) ActionListener :->Defines one method to receive action events.
  • (^) AdjustmentListener :->Defines one method to receive adjustment events.
  • (^) ComponentListener :->Defines four methods to recognize when a component is hidden, moved, resized, or shown.
  • (^) ContainerListener :->Defines two methods to recognize when a component is added to or removed from a container.
  • (^) FocusListener :->Defines two methods to recognize when a component gains or loses keyboard focus.
  • (^) ItemListener :->Defines one method to recognize when the state of an item changes.
  • (^) KeyListener :->Defines three methods to recognize when a key is pressed, released, or typed.
  • (^) MouseListener :->Defines five methods to recognize when the mouse is clicked, enters a component, exits a component, is pressed, or is released.
  • (^) MouseMotionListener :->Defines two methods to recognize when the mouse is dragged or moved.

The KeyEvent Class:

  • (^) A KeyEvent is generated when keyboard input occurs.
  • (^) There are three types of key events, which are identified by these integer constants: KEY_PRESSED, KEY_RELEASED, and KEY_TYPED.
  • (^) The first two events are generated when any key is pressed or released.
  • (^) The last event occurs only when a character is generated.

The KeyListener Interface:

  • (^) This interface defines three methods.
  • (^) The keyPressed( ) and keyReleased( ) methods are invoked when a key is pressed and released, respectively.
  • (^) The keyTyped( ) method is invoked when a character has been entered.
  • (^) The general forms of these methods are shown here:
  1. void keyPressed(KeyEvent ke)
  2. void keyReleased(KeyEvent ke)
  3. void keyTyped(KeyEvent ke)
  • (^) public void init() { addKeyListener(this); }
  • (^) public void keyPressed(KeyEvent ke) { showStatus("Key Down"); }
  • (^) public void keyReleased(KeyEvent ke) { showStatus("Key Up"); }
  • (^) public void keyTyped(KeyEvent ke)
  • (^) {
  • (^) msg += ke.getKeyChar();
  • (^) repaint();
  • (^) }
  • (^) // Display keystrokes.
  • (^) public void paint(Graphics g)
  • (^) {
  • (^) g.drawString(msg, X, Y);
  • (^) }
  • (^) }
  • (^) void mouseReleased(MouseEvent e) : Mouse key is released
  • (^) void mouseClicked(MouseEvent e) : Mouse key is pressed/released
  • (^) void mouseExited(MouseEvent e) : Mouse exited the component
  • (^) void mouseEntered(MouseEvent e) : Mouse entered the component
  • (^) void mousepressed(MouseEvent e) : Mouse key is pressed
  • (^) There are two types of events that MouseMotionListener can generate.
  • (^) There are two abstract functions that represent these five events.
  • (^) The abstract functions are
  • (^) void mouseDragged(MouseEvent e) : Invoked when a mouse button is pressed in the component and dragged. Events are passed until the user releases the mouse button.
  • (^) void mouseMoved(MouseEvent e) : invoked when the mouse cursor is moved from one point to another within the component, without pressing any mouse buttons.