EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi: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
Events User ActionSource ObjectEvent Type Fired Click a buttonJButtonActionEvent Press return on text fieldJTextFieldActionEvent Select a new itemJComboBoxItemEvent, ActionEvent Select item(s)JListListSelectionEvent Click a check boxJCheckBoxItemEvent, ActionEvent Click a radio buttonJRadioButtonItemEvent, ActionEvent Select a menu itemJMenuItemActionEvent Move the scroll barJScrollBarAdjustmentEvent Window opened, closed, iconified, deiconified, or closing WindowWindowEvent Mouse pressed, released, clicked, entered, or exited ComponentMouseEvent Bina Nusantara
Events User ActionSource ObjectEvent Type Fired Mouse moved or draggedComponentMouseEvent Key released or pressedComponentKeyEvent Component added or removed from the container ContainerContainerEvent Component moved, resized, hidden, or shown ComponentComponentEvent Component gained or lost focusComponentFocusEvent 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 EventsListenersMethods ActionEventActionListeneractionPerformed(ActionEvent) ItemEventItemListeneritemStateChanged(ItemEvent) MouseEventMouseListener MouseMotionListener mousePressed(MouseEvent) mouseReleased(MouseEvent) mouseEntered(MouseEvent) mouseExited(MouseEvent) mouseClicked(MouseEvent) mouseDragged(MouseEvent) mouseMoved(MouseEvent) KeyEventKeyListenerkeyPressed(KeyEvent) keyReleased(KeyEvent) keyTyped(KeyEvent) WindowEventWindowListenerwindowClosing(WindowEvent) windowOpened(WindowEvent) windowIconified(WindowEvent) Bina Nusantara
Listeners EventsListenersMethods WindowEventWindowListenerwindowDeiconified(WindowEvent) windowClosed(WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent) ContainerEventContainerListenercomponentAdded(ContainerEvent) componentRemoved(ContainerEvent) ComponentEventComponentListenercomponentMovied(ComponentEvent) componentHidden(ComponentEvent) componentResized(ComponentEvent) componentShown(ComponentEvent) FocusEventFocusListenerfocusGained(FocusEvent) focusLost(FoucusEvent) AdjustmentEventAdjustmentListeneradjustmentValueChanged(Adjustment Event) Bina Nusantara
User Actions Bina Nusantara Actions: Press Key Click Mouse Click Button Select Menu Events: Key Event Mouse Event Action Event PROGRAM causes notify Listen Handle User respond action User click show
ActionListener Bina Nusantara A simple program to demonstrate Event 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
MouseMotionListener A simple program to show how to use MouseMotionListener Result Bina Nusantara
WindowListener
A simple program to show how to use WindowListener Result
Bina Nusantara References Introduction to Java Programming. 7ed. Liang Chapter 15. Event-driven Programming. Wikipedia ActionListener. Sun html html KeyListener. Sun ml ml MouseMotionListener. Sun istener.html istener.html WindowListener. Sun er.html er.html