Presentation is loading. Please wait.

Presentation is loading. Please wait.

Listener Interfaces  A mechanism for registering handlers that respond to external events  Motivation: Develop a Canvas class that:  supports drawing.

Similar presentations


Presentation on theme: "Listener Interfaces  A mechanism for registering handlers that respond to external events  Motivation: Develop a Canvas class that:  supports drawing."— Presentation transcript:

1 Listener Interfaces  A mechanism for registering handlers that respond to external events  Motivation: Develop a Canvas class that:  supports drawing of 2D primitives (lines, circles, rectangles)  detects input events (i.e. events triggered by the mouse, keyboard, etc.)  allows users to handle these events based on their application

2 Listener Interfaces  A mechanism for registering handlers that respond to external events  Motivation: Develop a Canvas class that:  supports drawing of 2D primitives (lines, circles, rectangles)  detects input events (i.e. events triggered by the mouse, keyboard, etc.)  allows users to handle these events based on their application

3 Listener Interfaces  A mechanism for registering handlers that respond to external events  Motivation: Develop a Canvas class that:  supports drawing of 2D primitives (lines, circles, rectangles)  detects input events (i.e. events triggered by the mouse, keyboard, etc.)  allows users to handle these events based on their application  How can we separate the implementation of the Canvas from the implementation of the user code for handling the events

4 Listener Interfaces  How can we separate the implementation of the Canvas from the implementation of the user code fro handling events  Use a “registration” mechanism of event handlers:  user provides an object/handler to be notified when an event occurs  the Canvas is only responsible for notifying the handler of the event  the actual code of processing the event is in the user-supplied handler

5 Listener Interfaces  How can we separate the implementation of the Canvas from the implementation of the user code fro handling events  Use a “registration” mechanism of event handlers:  user provides an object/handler to be notified when an event occurs  the Canvas is only responsible for notifying the handler of the event  the actual code of processing the event is in the user-supplied handler  Need a set of agreed upon methods that handler must implement  Implication: The Canvas class only knows how to communicate with handler, not what handler does

6 Java Swing Example  MouseListener: set of agreed upon methods for mouse input handling: interface MouseListener { void mouseClicked(MouseEvent e); void mouseEntered(MouseEvent e);...... }  MouseEvent: “data structure” for describing the event  JPanel: representation of Canvas from Java Swing package  registration: void addMouseListener(MouseListener l)  notification: void processMouseEvent(MouseEvent e)

7 Java Swing Example  User creates a class that implements MouseListener interface class MyListener implements MouseListener { void mouseClicked(MouseEvent e) {... User-specific code for event handling... } void mouseEntered(MouseEvent e) {... User-specific code for event handling... }...... }

8 Java Swing Example panel listeners handler JPanel panel = new JPanel(“Demo”); MyListener handler = new MyListener(); panel.addMouseListener(handler);

9 J2ME Form Example  CommandListener: set of agreed upon methods for command handling interface CommandListener { void commandAction(Command c, Displayable d); }  Command: “data structure” for describing the event  Form: the application view  registration: void setCommandListener (CommandListener l)  notification: some method in Form class

10 J2ME Form Example  User creates a class that implements CommandListener interface class MyListener implements CommandListener { void commandAction(Command c, Displayable d) {... User-specific code for event handling... }

11 J2ME form Example panel listeners handler Form panel = new Form(“Demo”); MyListener handler = new MyListener(); panel.setCommandListener(handler);

12 J2ME User Interface Library Displayable ScreenCanvas FormAlertListTextbox Item DateFieldImageItemTextFieldStringItemGaugeChoiceGroup

13 Lab Exercise  Create a simple Form with a TextField and three Commands:  Exit – quits the application  Add Item – shows the text in the TextField on the Form  Delete Item – removes the last text item from the Form


Download ppt "Listener Interfaces  A mechanism for registering handlers that respond to external events  Motivation: Develop a Canvas class that:  supports drawing."

Similar presentations


Ads by Google