Title: Event Handling 2
1Event Handling 2
2General Idea
- You can register just about any component to
respond to events of some kind - May want to review previous slides before
continuing - Common components
- Buttons
- Applets
- TextFields and TextAreas
- Menus, ChoiceLists, CheckBoxes, etc...
3The Event Classes
- Mother class EventObject - defined in java.util
package - Below that is AWTEvent - defined in java.awt
package - Below that are the classes youve seen before
- MouseEvent, ActionEvent, TextEvent, KeyEvent,
WindowEvent, etc...
4Things in EventObjectThat you inherit
- getSource( ) gets the object that originated the
event (such as a button or textfield) - getID( ) returns the ID of an event such as
MOUSE_CLICKED. - Not used as often, because we rely on methods to
determine the event - Was more popular in 1.0 event model
5List of Event Types
- ActionEvent occurs by activation of components
(like a button, textfield or menu) - AdjustmentEvent works with scrollbars
- ComponentEvent high-level event thats rarely
used - ContainerEvent when components are added to a
container - InputEvent high-level event from input
6List of Event Types
- ItemEvent event when an item is selected
- KeyEvent event from the keyboard
- MouseEvent event from the mouse
- PaintEvent event when components are painted
(rarely used) - TextEvent when textfields are changed
- WindowEvent from window activity
7How to Listen for Events
- Has this format
- ltcomponentgt.addXListener (ltlistenergt)
This is the thing thats responsible for handling
the event. It must implement the interface. If
we added a mouseListener to a componenent, then
the listener must implement mouseListener
This is where you add a particular listener,
where X is a kind of listener. Example mouseListe
ner
This is the component thats interested
in reacting to events. Example a
button, textfield, menus, applets, frames, etc
8More Abstraction
- We can create separate classes as listener
- class MouseHandler implements MouseMotionListener
-
- public void mouseMoved (MouseEvent e)
- System.out.println (Mouse was moved)
-
- public void mouseDragged (MouseEvent e)
- System.out.println (Mouse was dragged)
-
9Now that we have a MouseHandler...
- Applet no longer has to implement the
MouseMotionListener interface - We can pass a new MouseHandler as the one
responsible for handling MouseEvents - Example
- MouseHandler x new MouseHandler( )
- this.addMouseMotionListener (x)
10The Listener Interfaces
- Important Ones
- ActionListener, AdjustmentListener, ItemListener,
KeyListener, MouseListener, MouseMotionListener,
TextListener, WindowListener
11AWTEvent Subclasses
- Important Ones ActionEvent, AdjustmentEvent,
ItemEvent, KeyEvent, MouseEvent, TextEvent,
WindowEvent
12Adapter Classes
- Its a pain to have to override methods when
youre only concerned with one thing - MouseListener 5 methods
- WindowListener 7 methods
- Adapter classes are classes that have overridden
the methods already - Can extend and Adapter class that has already
implemented this interface! - Is there an ActionAdapter?
13Guts of an Adapterby Example
- class MouseAdapter implements MouseListener
- public void mousePressed (MouseEvent e)
- public void mouseReleased(MouseEvent e)
- public void mouseClicked (MouseEvent e)
- public void mouseEntered(MouseEvent e)
- public void mouseExited (MouseEvent e)
- //end class
14More about Adapters
- class MyMouseHandler extends MouseAdapter
-
- public void mouseEntered (MouseEvent e)
-
- System.out.println (Mouse entered
the component) -
- //end class
Note now you can use MyMouseHandler to listen to
events. It has inherited 5 methods and overridden
the one that it wanted!
15Adapter Example Revisited
- class MyFrame extends Frame
- MyFrame(String title)
- super (title)
- init( )
-
- public void init( )
- this.addWindowListener (new WindowAdapter( )
- public void windowClosing (WindowEvent e)
- System.exit (1)
-
- )
-
16(No Transcript)