event-driven programming subject:t0934 / multimedia programming foundation session:2 tahun:2009...

19
EVENT-DRIVEN PROGRAMMING Subject : T0934 / Multimedia Programming Foundation Session : 2 Tahun : 2009 Versi : 1/0

Upload: kevin-malcolm-bradley

Post on 18-Jan-2018

221 views

Category:

Documents


0 download

DESCRIPTION

Bina Nusantara Course Outlines Events Listeners ActionListener KeyListener MouseMotionListener WindowListener

TRANSCRIPT

Page 1: EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0

EVENT-DRIVEN PROGRAMMING

Subject : T0934 / Multimedia Programming FoundationSession : 2Tahun : 2009Versi : 1/0

Page 2: 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

Page 3: EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0

Bina Nusantara

Course Outlines

• Events• Listeners• ActionListener• KeyListener• MouseMotionListener• WindowListener

Page 4: EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0

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

Page 5: EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0

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

Page 6: EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0

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

Page 7: EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0

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

Page 8: EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0

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

Page 9: EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0

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

Page 10: EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0

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

Page 11: EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0

ActionListener

Bina Nusantara

A simple program to demonstrateEvent Driven Programming

Result

Page 12: EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0

KeyListener

show

User

pressed

Page 13: EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0

KeyListener

Bina Nusantara

Page 14: EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0

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

Page 15: EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0

MouseMotionListener

move

drag

click and hold

Page 16: EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0

MouseMotionListenerA simple program to show how to use MouseMotionListener

Result

Bina Nusantara

Page 17: EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0

WindowListener

Page 18: EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0

WindowListenerA simple program to show how to use WindowListener

Result

Page 19: EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0

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