The Java 2 event model is the delegation model.
There are three participant in this event mechanism:
|
||||||
| Prior event model had one general purpose class which handled events: java.awt.Event.
Java 2 uses a hierarchy of java classes to handle events. The parent of all events is the: java.util.EventObject. |
||||||
| With the delegation model programmers have control over which events a Component will generate. | ||||||
| The delegation uses the observer-observable design pattern. The observable is the component that generates the event. The Observer is the object that has registered to receive the event. Class implementing the listeners interface are the observer objects. | ||||||
| The event source create event object and deliver them to event listener.
The event object is the medium used to deliver state information. |
||||||
| Event Object | The event object contain reference to the object that caused the event (the event source).
It may contain mouse position or a time stamp. |
|||||
| EventObject | The root class of all event class.
java.util.EventObject |
|||||
| It has one variable used to reference the source object. | ||||||
| To get the source object use this method:
public Object getSource() |
||||||
| Defining an event. The class of the event defines the event type.
A Foo event is declared as such: |
||||||
| public class FooEvent extends EventObject {
public FooEvent(Object source) { super(source); } } |
||||||
| Event Sources | Its an object or component that generates events. Their responsibility are as follows: | |||||
| Provide method allowing object to add or remove themselves as listeners. | ||||||
| Maintain a list of interested listeners. | ||||||
| Contain logic to create and deliver event object to appropriate listeners. | ||||||
| public class FooEventGenerator {
public void addFooListener(FooListener l) { //Code to add listener to the list of listeners } public void removeFooListener(FooListener l) { //Code to remove listener to the list of listeners } public void notifyFooListener(FooListener l) { //Create a FooEvent with this as the source FooEvent fooEvent = new FooEvent(this); //Deliver the event … } } |
||||||
| Event Listeners | For an object to receive a particular type of event, the object must implement the appropriate listener interface. | |||||
| EventListener | All listener interface are subclass of the EventListener.
java.util.EventListener |
|||||
| It has not methods, but is used as marker interface. | ||||||
| Subclass of EventListener interface methods usually take an EventObject as a paremeter. | ||||||
| public interface FooListener extends java.util.EventListener {
public void handleFooEvent(FooEvent e); } |
||||||
| Implementing the FooListener: | ||||||
| public class FooTest implements FooListener {
//Implement the FooListener interface public void handleFooEvent(FooEvent evt) { System.out.println(“Fooevent”); } } |
||||||
| Setting up an event source to receive the event: | ||||||
| //1. Create the event source
FooEventGenerator eventSource = new FooEventGenerator() //2. Create a listener FooTest listener = new FooTest(); //3. Register the listener with the event source eventtsource.addFooListener(listener); //4. To unregister event eventsource.removeFooListener(listener); |
||||||
| Event Package | java.awt.event Package | |||||
| The java.awt.event package contains all the events, listener and adapter class for the AWT components. | ||||||
| AWTEvent | The base class of the AWT events:
java.awt.AWTEvent. |
|||||
| It is a subclass of java.util.EventObject. AWTEvent is in the java.awt package not java.awt.event. Its methods: | ||||||
| The AWTEvent class adds two vars to the EventObject hierarchy: | ||||||
| int id
boolean consumed |
The id constant for an event.
Flag if event was consumed by the source component. |
|||||
| public int getID() | Returns the event type as an integer. Each AWTEvent has integer event type. | |||||
| protected void consume() | Consume the event. This keep and event from being further processed by further event source components. | |||||
| protected boolean isConsumed() | Test to see if event has been consumed. | |||||
| public String paramString() | Provide debug information. | |||||
| Events | Events in the java.awt.event package are of two types: semantic and low-level | |||||
| semantic | Semantic events occurs as a result of combination of low-level events.
Their concern with the primary purpose of the component. |
|||||
| low-level | Low-level are finer grained and tend to occur at a higher frequency. | |||||
| Semantic Events | There are three semantic events:
ActionEvent ItemEvent TextEvent |
|||||
| ActionEvent | They occur when:
List component is double-click Button component is clicked MenuItem component is selected TextField component enter key is pressed |
|||||
| Exam Tip | For List single click generates an ItemEvent, not ActionEvent. Single click select the item. Double click get’s its value. | |||||
| public String getActionCommand() | Returns a command string for the ActionEvent object. Contains information about the source or cause of the event. | |||||
| public int getModifiers() | Provide information about modifier keys: SHIFT, ALT.
Returns the integer modifier field for the ActionEvent object. |
|||||
| TextEvent | When test is changed in the component below the TextEvent is generated:
TextField TextArea |
|||||
| ItemEvent | The event occurs when:
Check box is selected. CheckboxMenuItem is selected or cleared. Choice item is selected List item is single-clicked |
|||||
| public Object getItem() | Returns the object selected. | |||||
| public ItemSelectable getItemSelectable() | Returns an ItemSelectable on the source component | |||||
| public int getStateChange() | Returns an int value of ItemEvent.SELECTED or ItemEvent.DESELECTED | |||||
| Low-Level Event | The majority of these event are component events, subclass of the java.awt.event.ComponentEvent. | |||||
| ComponentEvent | A component generates a ComponentEvent when a component is size, position, visibility has change.
It is the base class for many other low-level events. |
|||||
| They are for notification purposes only. | ||||||
| Exam Tip | Here are some of the subclasses of the ComponentEvent: | |||||
| ContainerEvent
PaintEvent WindowEvent |
FocusEvent
InputEvent MouseEvent, KeyEvent |
|||||
| public Component getComponent() | Get’s the source of the component. It is the same as the getSource() object of the EventObject. | |||||
| Similar events simplify the return of the object without casting. | ||||||
| ItemEvent
WindowEvent ContainerEvent |
getItemSelectable()
getWindow() getContainer() |
|||||
| ContainerEvent | When a child component of a container is add or removed a ContainerEvent is generated.
There are for notification purposes only. |
|||||
| public Component getChild() | Get the affected child component. | |||||
| public Container getContainer() | Returns an event source as Container. | |||||
| FocusEvent | When component gains or loses focus a FocusEvent is generated. | |||||
| InputEvent | InputEvent is an abstract class.
MouseEvent and KeyEvent are its subclass.
The methods of the InputEvent are as follows:
Input events are delivered to the listener before being handled by the component. This allows the listener to consume the event and keep it from being processed. |
|||||
| public int getModifiers() | Returns modifier flag as an int. There constant used to define the int value returned by this modifier. The InputEvent modifiers are: SHIFT_MASK, ALT_MASTK, CRTL_MASK.. | |||||
| Exam Tip | For mouse events the getModifier() can be used to determine which mouse was presssed. | |||||
| public isAltDown()
public isControlDown() public isMetaDown() public isShiftDown() |
Test for ALT, Control, Meta, or Shift key pressing. | |||||
| Exam Tip | getWhen() | Returns the time a key or mouse event occured. | ||||
| KeyEvent | There are two types of key event: key-typed, key-pressed | |||||
| key-typed | Key typed events are at a higher-level than pressed key events. They occur in combination of key actions. Example:SHIFT-A. | |||||
| public char getKeyChar() | Returns the character representation of a key typed. | |||||
| key pressed | Key pressed events occur for keys not associated with character, DEL, Function keys. | |||||
| public int getKeyCode() | Get the virtual code of the key pressed. | |||||
| MouseEvent | Mouse events have both a MouseListener and MouseMotionListener. | |||||
| Exam Tip | There is no mouse MouseMotionEvent | |||||
| public int getClickCount() | Returns the number of mouse clicks. | |||||
| public boolean isPopupTrigger() | True if the button pressed is the pop-up menu trigger. | |||||
| public int getX() | Get’s the x coordinate of the mouse location. | |||||
| public int getY() | Get’s the y coordinate of the mouse location. | |||||
| public Point getPoint() | Returns the mouse locations as a point. | |||||
| WindowEvent | When a window closes, opened, activated, iconized a WindowEvent is generated. | |||||
| public Window getWindow() | Get the source window. | |||||
| AdjustmentEvent | When a Scrollbar and ScrollPane is adjusted and AdjustmentEvent is generated. | |||||
| PaintEvent | Ensures serialization of paint operations of AWT event thread. | |||||
| Processing Event | The first method that who gets a chance to handle an event is the processEvent() method in the originating component. processEvent is defined in the java.awt.Component class. | |||||
| public void processEvent( AWTEvent e) | ||||||
| It is usually overridden to provide customized processing. | ||||||
| The default behavior of the processEvent is to determine the event type and call of the specialized methods as follows:
public void processFocusEvent (FocusEvent e) public void processMouseEvent(MouseEvent e) … |
||||||
| These methods normally send the event to the correct method of any listener that has registered with the component. | ||||||
| Enabling Event | Each event have an event mask that are 64bit long defined in the java.awt.AWTEvent class.
Enabling, disabling are primarily used int the processEvent() method. |
|||||
| Events can be explicitly enable or disabled by subclasses of java.awt.Component using these methods: | ||||||
| protected void enableEvents(long eventToEnable) | Turns off the bitmask for the event. | |||||
| protected void disableEvents(long eventToEnable) | Turns on the bitmask for the event. | |||||
| Listener Interface | Most event classes have a correspondent listener interface.
All listener interface are in the java.util.event package. There are 13 listener interface. |
|||||
| ActionEvent
AdjustmentEvent AWTEvent ComponentEvent ContainerEvent FocusEvent InputMethodEvent ItemEvent KeyEvent MouseEvent
TextEvent WindowEvent |
ActionListener
AdjustmentListener AWTListener ComponentListener ContainerListener FocusListener InputMethodListener ItemListener KeyListener MouseListener MouseMotionListener TextListener WindowListener |
|||||
| Exam Tip | These events do not have listeners:
PaintEvent InvocationEvent InputEvent |
|||||
| Adapter Classes | To make life easier adapter classes are provided for all the interfaces with multiple methods to be implemented. The adapter class provide empty implementation.
Note: abstract classes of theAWT have listeners. |
|||||
| Listeners with Adapter classes | Listeners with no Adapter classes(
interfaces have only one method). |
|||||
| ComponentAdapter
ContainerAdapter KeyAdapter MouseAdapter MouseMotionAdapter WindowAdapter
(WCKM) – memory |
ActionListener
TextListener ItemListener FocusListener AdjustmentListener AWTListener InputMethodListener
(TAIF) – memory |
|||||
| Exam Tip | Know which listeners don’t hava adapters | |||||
| Event Name | The naming arrangement of events are a follows: | |||||
| Decide on the Event xxxx (Ex: xxxx = Mouse event)
Implement its listener: xxxxListener (MouseListener) listenermethod(xxxEvent) Ex: MousePressed( MouseEvent ) Register the event: addxxxListener( listener ) Ex: source.addMouseListener(listener) |
||||||