INFT316 Event Handling - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

INFT316 Event Handling

Description:

The Observer model is a state change notification system ... The AWT and Swing APIs define a set of interfaces called listeners ... – PowerPoint PPT presentation

Number of Views:77
Avg rating:3.0/5.0
Slides: 13
Provided by: jamesB52
Category:
Tags: event | handling | inft316 | make

less

Transcript and Presenter's Notes

Title: INFT316 Event Handling


1
INFT316Event Handling
Tables from Wigglesworth and Lumby prepared by Dr
Michael Rees
2
Observers
  • The Observer model is a state change notification
    system
  • Objects can monitor other objects for change
  • Monitoring objects must implement interface
    Observer
  • void update( Observable obj, Object arg )
  • Monitored objects must extend class Observable
  • void addObserver( Observer obj )
  • void notifyObservers( )
  • void notifyObservers( Object obj )
  • void setChanged( )
  • boolean hasChanged( )

3
Observer Example
  • public class Pump
  • public static void main ()
  • Pump p new Pump()
  • Valve v new Valve()
  • Buzzer b new Buzzer()
  • v.addObserver(b)
  • public class Buzzer implements Observer
  • public void update ( Observable obj, Object arg)
  • Valve v (Valve) obj
  • if (v.getPressure( ) gt threshold )

4
AWT and Swing Components
5
Painting Swing Components
  • Painting renders the graphics image of JFC
    components
  • All paint operations controlled by the central
    event dispatch thread
  • Painting occurs when display is updated, e.g.,
  • window re-sized
  • window covered or uncovered by another
  • window maximised
  • Swing painting
  • All Swing components go in the containers
    contentPane (a JPanel)
  • Therefore
  • public void paint(Graphics g)
  • this.getContentPane().paint(g)
  • // your paint code here
  • or
  • put some central component on the content pane
    for drawing upon, e.g., a Canvas or JPanel
  • or
  • make the containers content pane your own JPanel

6
Event-driven Programming
  • The Hollywood model Dont call us, well call
    you.
  • Rather than continuously polling the system for
    events, a better model is to have user events
    trigger certain actions
  • Requires Events and Event handlers
  • Two steps required to handle an event
  • Define a class that listens for an occurrence of
    the event (by implementing the appropriate
    listener interface)
  • Register an instance of the listener class with
    the component affect by the event

7
Events
  • package java.awt.event
  • all AWT events
  • class java.awt.event.AWTEvent
  • superclass of all AWT events
  • class java.util.EventObject
  • superclass of all Java platform events
  • package javax.swing.event
  • swing events
  • mostly subclasses of java.util.EventObject
  • Common events
  • AdjustmentEvent for Adjustable object, eg
    JScrollBar
  • ComponentEvent move, show, hidden, or resize
  • ContainerEvent notification events
  • FocusEvent gets or loses focus
  • InputEvent superclass of mouse and keyboard
    events
  • ItemEvent eg JCheckBox, JComboBox and Jlist
  • KeyEvent
  • MouseEvent
  • PaintEvent needs update() call
  • TextEvent eg JTextField and JTextArea
  • WindowEvent
  • ChangeEvent for notification
  • InternalFrameEvent
  • ListSelectionEvent
  • MenuEvent
  • TableModelEvent
  • TreeSelectionEvent

8
Select Methods of the Event Classes
9
Select Methods of the Event Classes
10
Listeners
  • The AWT and Swing APIs define a set of interfaces
    called listeners
  • Each kind of event has a listener interface, and
    each listener has methods for every event that
    can occur in its event class
  • Use listener interfaces to create handlers for
    events

http//java.sun.com/j2se/1.4.2/docs/api/java/awt/e
vent/MouseListener.html
11
Listener Example
  • public class MyMouseListener implements
    MouseListener
  • public void mouseClicked(MouseEvent e)
  • Component clicked e.getComponent()
  • clicked.setBackground(new Color((float)
    Math.random(), (float) Math.random(), (float)
    Math.random()))
  • // need to implement all methods
    of interface

12
Listener Shortcuts
Write a Comment
User Comments (0)
About PowerShow.com