download

EVENT-DRIVEN
PROGRAMMING
Subject
Session
Tahun
Versi
:
:
:
:
T0934 / Multimedia Programming Foundation
2
2009
1/0
Learning Outcomes
In the end of this session, students must
be able to:
– Recognize Events and Listeners
– Understand concept of Event-Driven
Programming
– Use Listeners in Java programming
Bina Nusantara
Course Outlines
•
•
•
•
•
•
Events
Listeners
ActionListener
KeyListener
MouseMotionListener
WindowListener
Bina Nusantara
Events
• A signal to the program that something has happened
• Triggered by external user actions (e.g. mouse
movements, button clicks, keystrokes), or internal
program activities (e.g. timer)
• Program can choose to respond or ignore
• Source object / component  on which an event is fired
or generated
Bina Nusantara
Events
User Action
Source Object
Event Type Fired
Click a button
JButton
ActionEvent
Press return on text field
JTextField
ActionEvent
Select a new item
JComboBox
ItemEvent, ActionEvent
Select item(s)
JList
ListSelectionEvent
Click a check box
JCheckBox
ItemEvent, ActionEvent
Click a radio button
JRadioButton
ItemEvent, ActionEvent
Select a menu item
JMenuItem
ActionEvent
Move the scroll bar
JScrollBar
AdjustmentEvent
Window opened, closed,
or closing
Window
WindowEvent
Mouse pressed, released,
exited
Component
MouseEvent
Bina Nusantara
Events
User Action
Source
Event Type Fired
Mouse moved or dragged
Component
MouseEvent
Key released or pressed
Component
KeyEvent
Component added or
container
Container
ContainerEvent
Component moved, resized,
shown
Component
ComponentEvent
Component gained or lost
Component
FocusEvent
Bina Nusantara
Listeners
• Java need 2 objects to rise Event-Driven
– A source object fires an event, and an object interested in the
event handles
– The object that handles the event risen by the source object is
called Listener
• Using Listener
– Each listener handles different types of event risen by the source
object
– The listener object must be registered by the source object
Example, in ActionEvent, use addActionListener() method
– The event is written in respective listener method
Example, in ActionEvent, use actionPerformed() method
Bina Nusantara
Listeners
Events
Listeners
Methods
ActionEvent
ActionListener
actionPerformed(ActionEvent)
ItemEvent
ItemListener
itemStateChanged(ItemEvent)
MouseEvent
MouseListener
mousePressed(MouseEvent)
mouseReleased(MouseEvent)
mouseEntered(MouseEvent)
mouseExited(MouseEvent)
mouseClicked(MouseEvent)
mouseDragged(MouseEvent)
mouseMoved(MouseEvent)
MouseMotionListener
KeyEvent
KeyListener
keyPressed(KeyEvent)
keyReleased(KeyEvent)
keyTyped(KeyEvent)
WindowEvent
WindowListener
windowClosing(WindowEvent)
windowOpened(WindowEvent)
windowIconified(WindowEvent)
Bina Nusantara
Listeners
Events
Listeners
Methods
WindowEvent
WindowListener
windowDeiconified(WindowEvent)
windowClosed(WindowEvent)
windowActivated(WindowEvent)
windowDeactivated(WindowEvent)
ContainerEvent
ContainerListener
componentAdded(ContainerEvent)
componentRemoved(ContainerEven
ComponentEven ComponentListene
componentMovied(ComponentEvent
componentHidden(ComponentEvent
componentResized(ComponentEven
componentShown(ComponentEvent
FocusEvent
FocusListener
focusGained(FocusEvent)
focusLost(FoucusEvent)
AdjustmentEven
AdjustmentListener adjustmentValueChanged(Adjustme
Bina Nusantara
User Actions
Actions:
• Press Key
• Click Mouse
• Click Button
• Select Menu
causes
Events:
• Key Event
• Mouse Event
• Action Event
• Action Event
PROGRAM
notify
Listen
Handle
respond
action
User
User
click
show
Bina Nusantara
ActionListener
A simple program to demonstrate
Event Driven Programming
Result
Bina Nusantara
KeyListener
User
pressed
show
KeyListener
Bina Nusantara
KeyListener
A simple program to show
how to make a KeyListener
Remember that, if you use a
Listener, you MUST override
all the listener methods
Result
Bina Nusantara
MouseMotionListener
move
click and hold
drag
MouseMotionListener
A simple program to
show how to use
MouseMotionListener
Result
Bina Nusantara
WindowListener
WindowListener
A simple program to
show how to use
WindowListener
Result
References
• Introduction to Java Programming. 7ed. Liang. 2009. Chapter 15.
• Event-driven Programming. Wikipedia. 2009.
http://en.wikipedia.org/wiki/Event-driven_programming
• ActionListener. Sun. 2008.
http://java.sun.com/docs/books/tutorial/uiswing/events/actionlistener.
html
• KeyListener. Sun. 2008.
http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.ht
ml
• MouseMotionListener. Sun. 2008.
http://java.sun.com/docs/books/tutorial/uiswing/events/mousemotionl
istener.html
• WindowListener. Sun. 2008.
http://java.sun.com/docs/books/tutorial/uiswing/events/windowlisten
er.html
Bina Nusantara