Event Handling 2 - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Event Handling 2

Description:

More Abstraction. We can create separate classes as listener ... It's a pain to have to override methods when you're only concerned with one thing ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 17
Provided by: jeffch8
Category:

less

Transcript and Presenter's Notes

Title: Event Handling 2


1
Event Handling 2
2
General 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...

3
The 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...

4
Things 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

5
List 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

6
List 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

7
How 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
8
More 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)

9
Now 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)

10
The Listener Interfaces
  • Important Ones
  • ActionListener, AdjustmentListener, ItemListener,
    KeyListener, MouseListener, MouseMotionListener,
    TextListener, WindowListener

11
AWTEvent Subclasses
  • Important Ones ActionEvent, AdjustmentEvent,
    ItemEvent, KeyEvent, MouseEvent, TextEvent,
    WindowEvent

12
Adapter 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?

13
Guts 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

14
More 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!
15
Adapter 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)
Write a Comment
User Comments (0)
About PowerShow.com