EventDriven Programming 1 Topics Eventdriven programming Event event

  • Slides: 25
Download presentation
Event-Driven Programming 1

Event-Driven Programming 1

Topics • • Event-driven programming. Event, event source, and event classes. Listener classes and

Topics • • Event-driven programming. Event, event source, and event classes. Listener classes and handling events. Listener objects: registration in the source object. Java. awt. event. Action. Event. Java. awt. event. Mouse. Event. Java. awt. event. Key. Event. Java. util. Timer : controlling animations. 2

Procedural vs. Event-Driven Programming • Procedural programming: all code is executed from start to

Procedural vs. Event-Driven Programming • Procedural programming: all code is executed from start to finish. • Event-driven programming: certain code is executed upon activation of events. 3

Events • Event: a signal to the program that something has occurred and should

Events • Event: a signal to the program that something has occurred and should be “handled. ” • Event: mouse movements, mouse clicks, keystrokes (external), or generated by the operating system (timer). 4

Event Classes 5

Event Classes 5

Event Information • Event object: contains whatever properties are pertinent to the event. •

Event Information • Event object: contains whatever properties are pertinent to the event. • Source object of the event: use get. Source() instance in the java. util. Event. Object class. • Subclasses of Event. Object deal with special types of events, such as button actions, window events, component events, mouse movements, and keystrokes. 6

Selected User Actions User Action Source Object Event Type Generated Click a button Click

Selected User Actions User Action Source Object Event Type Generated Click a button Click a check box Click a radio button javax. swing. JButton JCheck. Box JRadio. Button Action. Event Item. Event, Action. Event Press return on a text field JText. Field Action. Event Select a new item JCombo. Box Item. Event, Action. Event Window opened, closed, etc. Window. Event Mouse pressed, released, etc. Key released, pressed, etc. Component Mouse. Event Key. Event 7

The Delegation Model 8

The Delegation Model 8

The Delegation Model: Listener. Class listener = new Listener. Class(); JButton jbt = new

The Delegation Model: Listener. Class listener = new Listener. Class(); JButton jbt = new JButton("OK"); jbt. add. Action. Listener(listener); 9

Selected Event Handlers Event Class Listener Interface Action. Event Item. Event Window. Event Action.

Selected Event Handlers Event Class Listener Interface Action. Event Item. Event Window. Event Action. Listener Item. Listener Window. Listener Container. Event Mouse. Event Key. Event Listener Methods (Handlers) action. Performed(Action. Event) item. State. Changed(Item. Event) window. Closing(Window. Event) window. Opened(Window. Event) window. Iconified(Window. Event) window. Deiconified(Window. Event) window. Closed(Window. Event) window. Activated(Window. Event) window. Deactivated(Window. Event) Container. Listener component. Added(Container. Event) component. Removed(Container. Event) java. awt. Event. Mouse. Listener mouse. Pressed(Mouse. Event) mouse. Released(Mouse. Event) mouse. Clicked(Mouse. Event) mouse. Exited(Mouse. Event) mouse. Entered(Mouse. Event) Key. Listener key. Pressed(Key. Event) key. Released(Key. Event) key. Typeed(Key. Event) 10

java. awt. event. Action. Event 11

java. awt. event. Action. Event 11

Handling Action Events • Display two buttons OK and Cancel in the window. •

Handling Action Events • Display two buttons OK and Cancel in the window. • Input: Click a button. • Output: Display a message on the console to indicate which button is clicked and when that event occurred. Test. Action. Event Run 12

Handling Window Events F Any subclass of the Window class can generate the following

Handling Window Events F Any subclass of the Window class can generate the following window events: window opened, closing, closed, activated, deactivated, iconified, and deiconified. F Input: a window event. F Output: a message to indicate the occurring event. Test. Window. Event Run 13

Multiple Listeners for a Single Source F. The two buttons OK and Cancel use

Multiple Listeners for a Single Source F. The two buttons OK and Cancel use the frame class as the listener. This example creates a new listener class as an additional listener for the action events on the buttons. When a button is clicked, both listeners respond to the action event. Test. Multiple. Listener Run 14

Mouse. Event 15

Mouse. Event 15

Handling Mouse Events • Java provides two listener interfaces, Mouse. Listener and Mouse. Motion.

Handling Mouse Events • Java provides two listener interfaces, Mouse. Listener and Mouse. Motion. Listener, to handle mouse events. • The Mouse. Listener listens for actions such as when the mouse is pressed, released, entered, exited, or clicked. • The Mouse. Motion. Listener listens for actions such as dragging or moving the mouse. 16

Handling Mouse Events 17

Handling Mouse Events 17

Moving Message Using Mouse Objective: Create a program to display a message in a

Moving Message Using Mouse Objective: Create a program to display a message in a panel. Use the mouse to move the message. The message moves as the mouse drags and is always displayed at the mouse point. Move. Message. Demo Run 18

Handling Complex Mouse Events Draw by dragging with the left mouse button pressed; erase

Handling Complex Mouse Events Draw by dragging with the left mouse button pressed; erase by dragging with the right button pressed. Scribble. Demo Run 19

Handling Keyboard Events To process a keyboard event, use the following handlers in the

Handling Keyboard Events To process a keyboard event, use the following handlers in the Key. Listener interface: • key. Pressed(Key. Event e) Called when a key is pressed. • key. Released(Key. Event e) Called when a key is released. • key. Typed(Key. Event e) Called when a key is pressed and then released. 20

The Key. Event Class • Methods: get. Key. Char() get. Key. Code() • Keys:

The Key. Event Class • Methods: get. Key. Char() get. Key. Code() • Keys: Name: Home VK_HOME End VK_End Page Up VK_PGUP Page Down VK_PGDN etc. . . 21

The Key. Event Class, cont. 22

The Key. Event Class, cont. 22

Keyboard Events Demo Display a userinput character. The user can also move the character

Keyboard Events Demo Display a userinput character. The user can also move the character up, down, left, and right using the arrow keys. Keyboard. Event. Demo Run 23

javax. swing. Timer Not all source objects are GUI components. The javax. swing. Timer

javax. swing. Timer Not all source objects are GUI components. The javax. swing. Timer class is a source component that fires an Action. Event at a predefined rate. The Timer class can be used to control animations: use it to display a moving message. Animation. Demo Run 24

Clock Animation Still. Clock shows the current time. The clock does not tick after

Clock Animation Still. Clock shows the current time. The clock does not tick after it is displayed. Make the clock display a new current time every second. Repaint it every second with a new current time. Use a timer to control how to repaint the clock. Clock. Animation Run 25