Upload
anbarasimurali
View
218
Download
0
Embed Size (px)
Citation preview
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 1/31
Event Handling
• An event handler is an asynchronous callback
subroutine that handles inputs received in a program.
Each event is a piece of application-level information
from the underlying framework, typically the GUItoolkit.
• This allows one component to identify to one or more
other components that an event (such as a user
pressing a button) has occurred so that the component
can respond to it.
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 2/31
The Delegation Event Model
• The modern approach to handling events is
based on the delegation event model
• Concept:
– a source generates an event and sends it to one or
more listeners.
– the listener simply waits until it receives an event.
– Once received, the listener processes the event
and then returns.
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 3/31
Events
In the delegation model, an event is an object that
describes a state change in a source.
For example, an event may be generated when a
timer expires, a counter exceeds a value, asoftware or hardware failure occurs, or an
operation is completed. We are free to define
events that are appropriate for your application.
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 4/31
Event Classes
• At the root of the Java event class hierarchy is
EventObject, which is in java.util.*
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 5/31
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 6/31
Event Class
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 7/31
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 8/31
Event Sources
• A source is an object that generates an 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 )
Type is the name of the event and
el is a reference to the event listener
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 9/31
• Example:
• The method that registers a keyboard event
listener is called addKeyListener( )
• The method that registers a mouse motion
listener is called addMouseMotionListener( )
• Note: When an event occurs, all registeredlisteners are notified and receive a copy of the
event object. This is known as multicasting the
event.
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 10/31
Event Source
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 11/31
Event Listeners
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.
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 12/31
Event Listener Interface
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 13/31
The ActionListener Interface
• void actionPerformed(ActionEvent ae)
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 14/31
The AdjustmentListener Interface
• Void
adjustmentValueChanged(AdjustmentEvent
ae)
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 15/31
The ComponentListener Interface
• void componentResized(ComponentEvent ce)
• void componentMoved(ComponentEvent ce)
•
void componentShown(ComponentEvent ce)• void componentHidden(ComponentEvent ce)
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 16/31
The ContainerListener Interface
• void componentAdded(ContainerEvent ce)
• void componentRemoved(ContainerEvent ce)
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 17/31
The FocusListener Interface
• void focusGained(FocusEvent fe)
• void focusLost(FocusEvent fe)
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 18/31
The ItemListener Interface
• void itemStateChanged(ItemEvent ie)
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 19/31
The KeyListener Interface
• void keyPressed(KeyEvent ke)
• void keyReleased(KeyEvent ke)
•
void keyTyped(KeyEvent ke)
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 20/31
The MouseListener Interface
• void mouseClicked(MouseEvent me)
• void mouseEntered(MouseEvent me)
•
void mouseExited(MouseEvent me)• void mousePressed(MouseEvent me)
• void mouseReleased(MouseEvent me)
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 21/31
The MouseMotionListener
Interface
• void mouseDragged(MouseEvent me)
• void mouseMoved(MouseEvent me)
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 22/31
The MouseWheelListener
Interface
• void mouseWheelMoved(MouseWheelEvent
mwe)
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 23/31
The TextListener Interface
• void textChanged(TextEvent te)
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 24/31
The WindowFocusListener
Interface
• void windowGainedFocus(WindowEvent we)
• void windowLostFocus(WindowEvent we)
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 25/31
The WindowListener Interface
• void windowActivated(WindowEvent we)
• void windowClosed(WindowEvent we)
•
void windowClosing(WindowEvent we)• void windowDeactivated(WindowEvent we)
• void windowDeiconified(WindowEvent we)
• void windowIconified(WindowEvent we)• void windowOpened(WindowEvent we)
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 26/31
Sample Program using Mouse
Event
l l d JP l i l M Li
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 27/31
• class mousepanel extends JPanel implements MouseListener
• {
• String msg = "";
• int mouseX = 0, mouseY = 0;
• public mousepanel()
• {addMouseListener(this);
• }
• public void mouseClicked(MouseEvent me) {
• // save coordinates
• mouseX = 50;
• mouseY = 50;
• msg = "Mouse clicked.";
• repaint();
• }
• // Handle mouse entered.• public void mouseEntered(MouseEvent me) {
• // save coordinates
• mouseX = 75;
• mouseY = 100;
• msg = "Mouse entered.";
• repaint();
• }
• // Handle mouse exited.
• public void mouseExited(MouseEvent me) {
• // save coordinates
• mouseX = 0;
• mouseY = 10;
• msg = "Mouse exited.";
• repaint();
•}
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 28/31
• public void mousePressed(MouseEvent me) {
• // save coordinates
• mouseX = me.getX();
• mouseY = me.getY();
•msg = "Down";
• repaint();
• }
• // Handle button released.
• public void mouseReleased(MouseEvent me) {
• // save coordinates
• mouseX = me.getX();• mouseY = me.getY();
• msg = "Up";
• repaint();
• }
• public void paint(Graphics g) {
• g.drawString(msg, mouseX, mouseY);• }
• }
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 29/31
• class newframe extends JFrame
• {
• public newframe()
• {
• setTitle("Mouse Event");
• setSize(100,300);
• mousepanel p1=new mousepanel();
• add(p1);
• }
• }
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 30/31
• import javax.swing.*;
• import java.awt.*;
•import java.awt.event.*;
• import java.util.*;
• class demo1
• {
• public static void main(String args[])• {
• newframe f1=new newframe();
• f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
• f1.setVisible(true);• }
• }
8/4/2019 Event Class
http://slidepdf.com/reader/full/event-class 31/31
Sample Output