Upload
dwayne-day
View
214
Download
0
Embed Size (px)
Citation preview
Event Handling
The signals that a program receives from the operating system as a result of the actions are called events.
A window based program is called an event driven program because the sequence of events are created as a result of the interaction with the GUI which determines what happens in the program.
Keystrokes, Mouse Actions
Operating System
Java Library Classes
Method Method MethodProgram
Event
Java Event Delegation Model In java events are objects. The java.awt.event package defines
hierarchy of event types. When an event occurs the programmer can
Ignore the event Handle the event by the component where
the event originated Delegate event handling to some other
object or objects, called listeners.
Ignoring the event
Do nothing
Handling the events in the originating components
A self-contained component is the one that handles the event that it generates.
For a component to handle its own events, a subclass needs to be created.
The subclass needs to do two things: Get ready to receive events, by calling its
enableEvents() method. Implement a processActionEvent() method
which will be called when an event is received.
The class AWTEvent is a subclass of java.util.EventObject. It defines a method getSource() which returns the object that
is the source of an event as type Object. The class AWTEvent defines constants which are public
final whose values identifying the various kinds of events. MOUSE_EVENT_MASK KEY_EVENT_MASK ITEM__EVENT_MASK WINDOW_EVENT_MASK MOUSE_MOTION_EVENT_MASK FOCUS_EVENT_MASK TEXT_EVENT_MASK ADJUSTMENT_EVENT_MASK• Each having of single bit.• They can be combined by using bitwise OR operator.
Enabling Events
enalbleEvent() is inherited by component class so any component can handle its own events.
Pass an argument defining the events which the component has to handle.
More than one events can be combined with bitwise OR opertaor ( | ).
E.g. a window that handles both mouse events and window events
enableEvents(AWTEvent.WINDOW_EVENT_MASK |AWTEvent.MOUSE_EVENT_MASK );
When an event is generated, the JVM generates an “action event” and then checks whether ActionEvent() is enabled.
If so then JVM calls processActionEvent() which takes one argument as the object of ActionEvent class.
Example
By passing different values to enableEvents(), a component can catch many different kinds of events.
All of the event handlers have names of the format processXXXEvent(), where XXX stands for the event type.
Delegating the event
An event always has a source object e.g. Button.
When the button is clicked, it’ll create a new object that represents and identifies this event.
This object will contain information about the event and its source.
Button clicked
OK
creates
ActionEvent Object
public class ActionHandler Implements ActionListener{
//class member//constructor…..
……button.addActionListener(this);
………..………
void actionPerformed(ActionEvent e){
//handle the event…….
}
}
The listener connectsto the event source by callingaddActionListener()method for the source object
Passed To
How to define a listener?
Variety of listener interfaces are available for different kinds of events. Interface Interface Methods AddMethodActionListener actionPerformed(ActionEvent) addActionListener()AdjustmentListener adjustmentValueChanged (AdjustmentEvent)
addAdjustmentListener()ComponentListener componentHidden(ComponentEvent)
componentMoved(ComponentEvent)componentResized(ComponentEvent)
addComponentListener()componentShown(ComponentEvent)
ContainerListener componentAdded(ContainerEvent)componentRemoved(ContainerEvent)
addContainerListener()FocusListener focusGained(FocusEvent)
focusLost(focusEvent) addFocusListener()ItemListener itemStateChanged(ItemEvent) addItemListener()KeyListener keyPressed(keyEvent)
keyReleased(keyEvent)keyTyped(keyEvent) addKeyListener()
MouseListener mouseClicked(MouseEvent)mouseEntered(MouseEvent)mouseExited(MouseEvent)mousePressed(MouseEvent)mouseReleased(MouseEvent) addMouseListener()
MouseMotionListener mouseDragged(MouseEvent)mouseMoved(MouseEvent)
addMouseMotionListener()
TextListener textValueChanged(TextEvent) addTextListener()WindowListener windowActivated(WindowEvent)
windowClosed(WindowEvent)windowClosing(WindowEvent)windowDeactivated(WindowEvent)windowDeiconified(WindowEvent)windowIconified(WindowEvent)windowOpened(WindowEvent) addWindowListener()
The steps to support event handling is as follows:
Create a listener class that implements the desired listener interface.
Construct the component. Construct an instance of the listener
class. Call addXXXListener() on the
component, passing in the listener object.
Example Example
Java Event Type ( java.awt.event)java.util.EventObject
java.awt.Awt.Event
ActionEvent AdjustmentEvent ItemEvent TextEvent
ComponentEvent
ContainerEvent FocusEvent PaintEvent WindowEvent
InputEvent
MouseEvent KeyEvent
Java.util.EventObject provides a method getSource() which returns the component in which the event took place.
Java.awt.AWTEvent provides a method getID() which returns an int value that describes the nature of event e.g. getID for MouseEvent results in int value of MouseEvent.Mouse_Pressed, MouseEvent.Mouse_Dragged, etc.
Adjustment Events
Sent by scrollbars.public class AdjustmentEvent extends AWTEvent{ public static final int BLOCK_DECREMENT; public static final int BLOCK_INCREMENT; public static final int TRACK; public static final int UNIT_DECREMENT; public static final int UNIT_INCREMENT; public AdjustmentEvent(Adjustable s, int id, int type, int value); public Adjustable getAdjustable(); public int getAdjustmentType(); public int getValue(); public String paramString();}public interface AdjustmentListener extends EventListener{ public void adjustmentValueChanged(AdjustmentEvent AdjEvt); }ExampleExample
Item Events
Generated by components that present users with items to choose from.
The components are- Choice, List, Checkbox,
CheckboxMenuItem.
public static final int DESELECTED;public static final int
ITEM_STATE_CHANGED;public static final int SELECTED;public ItemEvent(ItemSelectable src, int id,
Object item, int stateChanged);public Object getItem();public ItemSelectable getItemSelectable();public int getStateChange();public String paramString();
A component can process its own item events by calling enableEvents(AWTEvent.ITEM_EVENT_MASK) and providing a processItemEvent() method. OR
Can delegate by-public interface ItemListener extends EventListener{ void itemStateChanged(ItemEvent e);}Example
Key Events
Key events are generated when a user presses or releases a key.
KEY_PRESSED indicates that a key was pushed down.
KEY_RELEASED indicates that a key was released.
KEY_TYPED denotes a key press followed by a key release.
public static String getKeyText(int keyCode); public char getKeyChar(); public int getKeyCode(); public boolean isActionKey(); public void setKeyCode(int keyCode); It handles own event by enableEvents(AWTEvent.KEY_EVENT_MASK) and
processKeyEvent() OR delegate by- public interface KeyListener extends EventListener { public void keyPressed(KeyEvent k); public void keyReleased(KeyEvent k); public void keyTyped(KeyEvent k); }
Mouse Events
public static final int MOUSE_DRAGGED; public static final int MOUSE_ENTERED; public static final int MOUSE_EXITED; public static final int MOUSE_MOVED; public static final int MOUSE_PRESSED; public static final int MOUSE_RELEASED;
public Point getPoint(); public int getX(); public int getY(); A mouse event is processed by calling
enableEvents(AWTEvent.MOUSE_EVENT_MASK) and processMouseEvent().
OR event can be delegated to listener by implementing MouseListener interface.
public interface MouseListener extends EventListener
{
public void mouseClicked(MouseEvent m);
public void mouseEntered(MouseEvent m);
public void mouseExited(MouseEvent m);
public void mousePressed(MouseEvent m);
public void mouseReleased(MouseEvent m);
}
Mouse-motion events are handled by- enableEvents(AWTEvent.MOUSE_MOTION_EVENT
_MASK) and processMouseMotionEvent() OR delegate bypublic interface MouseMotionListener extends
EventListener{ public void mouseDragged(MouseEvent m); public void mouseMoved(MouseEvent m);}
Window Events
public class WindowEvent extends ComponentEvent{ public static final int WINDOW_ACTIVATED; public static final int WINDOW_CLOSED; public static final int WINDOW_CLOSING; public static final int WINDOW_DEACTIVATED; public static final int WINDOW_DEICONIFIED; public static final int WINDOW_ICONIFIED; public static final int WINDOW_OPENED; public WindowEvent(Window src, int id); public Window getWindow(); public String paramStrings();}
Adapters
An adapter class provides an empty implementation of all methods in an event listener interface.
Adapter classes are useful when you want to receive and process only some of the events that are handled by a particular event listener interface.
class MyMouse implements MouseListener { public void mouseClicked(MouseEvent me) { } }
This class won't compile because you haven't declared the other methods of the interface; mouseEntered(MouseEvent), mouseExited(MouseEvent), etc...
So you use an adapter:
class MyMouse extends MouseAdapter { public void mouseClicked(MouseEvent me) { } }
The compiler won't complain now because the superclass MouseAdapter already provides do-nothing methods for the interface implementation.
Adapter Class Listener Interface
ComponentAdapter ComponentListener
ContainerAdapter ContainerListener
FocusAdapter FocusListener
KeyAdapter KeyListener
MouseAdapter MouseListener
MouseMotionAdapterMouseMotionListener
WindowAdapter WindowListener
Example Example