Download presentation
Presentation is loading. Please wait.
Published byKevin Malcolm Bradley Modified over 9 years ago
1
EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0
2
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
3
Bina Nusantara Course Outlines Events Listeners ActionListener KeyListener MouseMotionListener WindowListener
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
ActionListener Bina Nusantara A simple program to demonstrate Event Driven Programming Result
12
KeyListener show User pressed
13
KeyListener Bina Nusantara
14
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
15
MouseMotionListener move drag click and hold
16
MouseMotionListener A simple program to show how to use MouseMotionListener Result Bina Nusantara
17
WindowListener
18
A simple program to show how to use WindowListener Result
19
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 http://en.wikipedia.org/wiki/Event-driven_programming ActionListener. Sun. 2008. http://java.sun.com/docs/books/tutorial/uiswing/events/actionlistener. html 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 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 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 http://java.sun.com/docs/books/tutorial/uiswing/events/windowlisten er.html
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.