EventDriven Programming Procedural vs EventDriven Programming F Procedural

  • Slides: 34
Download presentation
Event-Driven Programming

Event-Driven Programming

Procedural vs. Event-Driven Programming F Procedural programming is executed in procedural order. F In

Procedural vs. Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.

Taste of Event-Driven Programming

Taste of Event-Driven Programming

Events F An event can be defined as a type of signal to the

Events F An event can be defined as a type of signal to the program that something has happened. The event is generated by external user actions such as mouse movements, mouse clicks, and keystrokes, or by the operating system, such as a timer. F Every time the user types a character or pushes a mouse button, an event occurs. Any object can be notified of the event. All it has to do is implement the appropriate interface and be registered as an event listener on the appropriate event source. GUIs are event driven F – Generate events when user interacts with GUI u e. g. , moving mouse, pressing button, typing in text field, etc. u Class java. awt. AWTEvent

Event Classes

Event Classes

Event Information An event object contains whatever properties are pertinent to the event. You

Event Information An event object contains whatever properties are pertinent to the event. You can identify the source object of the event using the get. Source() instance method in the Event. Object class. The subclasses of Event. Object deal with special types of events, such as button actions, window events, component events, mouse movements, and keystrokes.

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 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 List of event

Event-Handling Model F Event-handling model – Three parts u Event source – GUI component

Event-Handling Model F Event-handling model – Three parts u Event source – GUI component with which user interacts u Event object – Encapsulates information about event that occurred u Event listener – Receives event object when notified, then responds – Programmer must perform two tasks Register event listener for event source u Implement event-handling method (event handler) u

The Delegation Model

The Delegation Model

Internal Function of a Source Component

Internal Function of a Source Component

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

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

Selected Event Handlers Event Class Listener Interface Listener Methods (Handlers) Action. Event Item. Event

Selected Event Handlers Event Class Listener Interface Listener Methods (Handlers) Action. Event Item. Event Window. Event Action. Listener Item. Listener Window. Listener Container. Event Container. Listener Mouse. Event Mouse. Listener Key. Event Key. Listener 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) component. Added(Container. Event) component. Removed(Container. Event) mouse. Pressed(Mouse. Event) mouse. Released(Mouse. Event) mouse. Clicked(Mouse. Event) mouse. Exited(Mouse. Event) mouse. Entered(Mouse. Event) key. Pressed(Key. Event) key. Released(Key. Event) key. Typeed(Key. Event)

java. awt. event. Action. Event

java. awt. event. Action. Event

Inner Class Listeners A listener class is designed specifically to create a listener object

Inner Class Listeners A listener class is designed specifically to create a listener object for a GUI component (e. g. , a button). It will not be shared by other applications. So, it is appropriate to define the listener class inside the frame class as an inner class.

Inner Classes Inner class: A class is a member of another class. Advantages: In

Inner Classes Inner class: A class is a member of another class. Advantages: In some applications, you can use an inner class to make programs simple. F An inner class can reference the data and methods defined in the outer class in which it nests, so you do not need to pass the reference of the outer class to the constructor of the inner class.

Inner Classes, cont.

Inner Classes, cont.

Inner Classes (cont. ) F Inner classes can make programs simple and concise. F

Inner Classes (cont. ) F Inner classes can make programs simple and concise. F An inner class supports the work of its containing outer class and is compiled into a class named Outer. Class. Name$Inner. Class. Name. class. For example, the inner class Inner. Class in Outer. Class is compiled into Outer. Class$Inner. Class. class.

Inner Classes (cont. ) F An inner class can be declared public, protected, or

Inner Classes (cont. ) F An inner class can be declared public, protected, or private subject to the same visibility rules applied to a member of the class. F An inner class can be declared static. A static inner class can be accessed using the outer class name. A static inner class cannot access nonstatic members of the outer class

Revising Simple. Event. Demo Using Inner Classes

Revising Simple. Event. Demo Using Inner Classes

Anonymous Inner Classes F F An anonymous inner class must always extend a superclass

Anonymous Inner Classes F F An anonymous inner class must always extend a superclass or implement an interface, but it cannot have an explicit extends or implements clause. An anonymous inner class must implement all the abstract methods in the superclass or in the interface. An anonymous inner class always uses the no-arg constructor from its superclass to create an instance. If an anonymous inner class implements an interface, the constructor is Object(). An anonymous inner class is compiled into a class named Outer. Class. Name$n. class. For example, if the outer class Test has two anonymous inner classes, these two classes are compiled into Test$1. class and Test$2. class.

Anonymous Inner Classes (cont. ) Inner class listeners can be shortened using anonymous inner

Anonymous Inner Classes (cont. ) Inner class listeners can be shortened using anonymous inner classes. An anonymous inner class is an inner class without a name. It combines declaring an inner class and creating an instance of the class in one step. An anonymous inner class is declared as follows: new Super. Class. Name/Interface. Name() { // Implement or override methods in superclass or interface // Other methods if necessary }

Revising Simple. Event. Demo Using Anonymous Inner Classes

Revising Simple. Event. Demo Using Anonymous Inner Classes

Example: Handling Simple Action Events F Objective: Display two buttons OK and Cancel in

Example: Handling Simple Action Events F Objective: Display two buttons OK and Cancel in the window. A message is displayed on the console to indicate which button is clicked, when a button is clicked.

Interaction Between Source and Listener 1. jbt. OK registers bt. Listener by invoking add.

Interaction Between Source and Listener 1. jbt. OK registers bt. Listener by invoking add. Action. Listener(bt. Listner). 2. jbt. Cancel registers bt. Listener by invoking add. Action. Listener(bt. Listner). 3. jbt. OK invokes bt. Listener’s action. Performed method to process an Action. Evnet. 4. jbt. Cancel invokes bt. Listener’s action. Performed method to process an Action. Event.

Example: Multiple Listeners for a Single Source F Objective: The two buttons OK and

Example: Multiple Listeners for a Single Source F Objective: 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. private class Second. Listener implements Action. Listener { public void action. Performed(Action. Event e) { System. out. print("First listener: "); private class Second. Listener implements Action. Listener { public void action. Performed(Action. Event e) { System. out. print("Second listener: "); if (e. get. Action. Command(). equals("OK")) { System. out. println("The OK button is clicked"); } else if (e. get. Action. Command(). equals("Cancel")) { System. out. println("The Cancel button is clicked"); } } if (e. get. Source() == jbt. OK) { System. out. println("The OK button is clicked"); } else if (e. get. Source() == jbt. Cancel) { System. out. println("The Cancel button is clicked"); } Action. Listener first. Listener = new First. Listener(); jbt. OK. add. Action. Listener(first. Listener); jbt. Cancel. add. Action. Listener(first. Listener); // Register the second listener for buttons Action. Listener second. Listener = new Second. Listener(); jbt. OK. add. Action. Listener(second. Listener); jbt. Cancel. add. Action. Listener(second. Listener);

Mouse. Event

Mouse. Event

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

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

Handling Mouse Events

Handling Mouse Events

Example: Handling Complex Mouse Events Objective: Create a program for drawing using a mouse.

Example: Handling Complex Mouse Events Objective: Create a program for drawing using a mouse. Draw by dragging with the left mouse button pressed; erase by dragging with the right button pressed. Scribble

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: F key. Pressed(Key. Event e) Called when a key is pressed. F key. Released(Key. Event e) Called when a key is released. F key. Typed(Key. Event e) Called when a key is pressed and then released.

The Key. Event Class F Methods: get. Key. Char() method get. Key. Code() method

The Key. Event Class F Methods: get. Key. Char() method get. Key. Code() method F Keys: Home VK_HOME End VK_END Page Up VK_PGUP Page Down VK_PGDN etc. . .

The Key. Event Class, cont.

The Key. Event Class, cont.

Example: Keyboard Events Demo Objective: Display a user-input character. The user can also move

Example: Keyboard Events Demo Objective: Display a user-input character. The user can also move the character up, down, left, and right using the arrow keys. Key Event

Clock Animation Still Clock Animation

Clock Animation Still Clock Animation