event-driven programming subject:t0934 / multimedia programming foundation session:2 tahun:2009...
DESCRIPTION
Bina Nusantara Course Outlines Events Listeners ActionListener KeyListener MouseMotionListener WindowListenerTRANSCRIPT
EVENT-DRIVEN PROGRAMMING
Subject : T0934 / Multimedia Programming FoundationSession : 2Tahun : 2009Versi : 1/0
Bina Nusantara
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
EventsUser Action Source
ObjectEvent Type Fired
Click a button JButton ActionEventPress return on text field JTextField ActionEventSelect a new item JComboBox ItemEvent,
ActionEventSelect item(s) JList ListSelectionEventClick a check box JCheckBox ItemEvent,
ActionEventClick a radio button JRadioButton ItemEvent,
ActionEventSelect a menu item JMenuItem ActionEventMove the scroll bar JScrollBar AdjustmentEventWindow opened, closed, iconified, deiconified, or closing
Window WindowEvent
Mouse pressed, released, clicked, entered, or exited
Component MouseEventBina Nusantara
EventsUser Action Source
ObjectEvent Type Fired
Mouse moved or dragged Component MouseEventKey released or pressed Component KeyEventComponent added or removed from the container
Container ContainerEvent
Component moved, resized, hidden, or shown
Component ComponentEvent
Component gained or lost focus
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 objectExample, in ActionEvent, use addActionListener() method
– The event is written in respective listener methodExample, in ActionEvent, use actionPerformed() method
Bina Nusantara
ListenersEvents Listeners MethodsActionEvent ActionListener actionPerformed(ActionEvent)ItemEvent ItemListener itemStateChanged(ItemEvent)MouseEvent MouseListener
MouseMotionListener
mousePressed(MouseEvent)mouseReleased(MouseEvent)mouseEntered(MouseEvent)mouseExited(MouseEvent)mouseClicked(MouseEvent)mouseDragged(MouseEvent)mouseMoved(MouseEvent)
KeyEvent KeyListener keyPressed(KeyEvent)keyReleased(KeyEvent)keyTyped(KeyEvent)
WindowEvent WindowListener windowClosing(WindowEvent)windowOpened(WindowEvent)windowIconified(WindowEvent)
Bina Nusantara
ListenersEvents Listeners MethodsWindowEvent WindowListener windowDeiconified(WindowEvent)
windowClosed(WindowEvent)windowActivated(WindowEvent)windowDeactivated(WindowEvent)
ContainerEvent ContainerListener componentAdded(ContainerEvent)componentRemoved(ContainerEvent)
ComponentEvent
ComponentListener
componentMovied(ComponentEvent)componentHidden(ComponentEvent)componentResized(ComponentEvent)componentShown(ComponentEvent)
FocusEvent FocusListener focusGained(FocusEvent)focusLost(FoucusEvent)
AdjustmentEvent
AdjustmentListener
adjustmentValueChanged(AdjustmentEvent)
Bina Nusantara
User Actions
Bina Nusantara
Actions:• Press Key• Click Mouse• Click Button• Select Menu
Events:• Key Event• Mouse Event• Action Event• Action Event
PROGRAMcauses notify
ListenHandle
User
respond
action
User
click
show
ActionListener
Bina Nusantara
A simple program to demonstrateEvent Driven Programming
Result
KeyListener
show
User
pressed
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
drag
click and hold
MouseMotionListenerA simple program to show how to use MouseMotionListener
Result
Bina Nusantara
WindowListener
WindowListenerA simple program to show how to use WindowListener
Result
Bina Nusantara
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.html
• MouseMotionListener. Sun. 2008. http://java.sun.com/docs/books/tutorial/uiswing/events/mousemotionlistener.html
• WindowListener. Sun. 2008. http://java.sun.com/docs/books/tutorial/uiswing/events/windowlistener.html