Presentation is loading. Please wait.

Presentation is loading. Please wait.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 Chapter 14 Event-Driven Programming.

Similar presentations


Presentation on theme: "Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 Chapter 14 Event-Driven Programming."— Presentation transcript:

1 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 Chapter 14 Event-Driven Programming

2 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 2 Objectives F To start with event-driven programming with a simple example (§14.1). F To explain the concept of event-driven programming (§14.2). F To understand events, event sources, and event classes (§14.2). F To declare listener classes and write the code to handle events (§14.3). F To register listener objects in the source object (§11.3). F To understand how an event is handled (§14.3). F To write programs to deal with ActionEvent (§14.3).

3 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 3 Procedural vs. Event-Driven Programming F Procedural programming is executed in procedural order.  In event-driven programming, code is executed upon activation of events.

4 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 4 Taste of Event-Driven Programming F The example displays a button in the frame. A message is displayed on the console when a button is clicked. SimpleEventDemo Run

5 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 5 Events F An event can be defined as a type of signal to the program that something has happened. F 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.

6 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 6 Event Classes

7 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 GUIs are event driven. F GUIs are event driven. F When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform a task. F The code that performs a task in response to an event is called an event handler, and the overall process of responding to events is known as event handling. 7

8 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 8 Event Information An event object contains whatever properties are pertinent to the event. You can identify the source object of the event using the getSource() instance method in the EventObject class. The subclasses of EventObject deal with special types of events, such as button actions, window events, component events, mouse movements, and keystrokes. Table 14.1 lists external user actions, source objects, and event types generated.

9 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 9 Selected User Actions SourceEvent Type User ActionObjectGenerated Click a button JButtonActionEvent Click a check box JCheckBoxItemEvent, ActionEvent Click a radio button JRadioButtonItemEvent, ActionEvent Press return on a text field JTextFieldActionEvent Select a new item JComboBoxItemEvent, ActionEvent Window opened, closed, etc. WindowWindowEvent Mouse pressed, released, etc. ComponentMouseEvent Key released, pressed, etc. ComponentKeyEvent

10 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 Event Source and Event Listeners Objects F Event handling is implemented by two types of objects: –event source objects –event listener objects 10

11 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 Event Source and Event Listeners Objects F An event source is a GUI object where an event occurs. We say an event source generates events. - Buttons, text boxes, list boxes, and menus are common event sources in GUI-based applications. F An event listener object is an object that includes a method that gets executed in response to the generated events. -A listener must be associated, or registered, to a source, so it can be notified when the source generates events. 11

12 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 Connecting Source and Listener - A listener must be registered to a event source. Once registered, it will get notified when the event source generates events. 12 JButton Handler event source event listener notify register

13 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 Event Types F There are different event types and event listeners u Mouse events are handled by mouse listeners u Item selection events are handled by Item listeners u and so forth F Among the different types of events, the action event is the most common. –Clicking on a button generates an action event –Selecting a menu item generates an action event –and so forth F Action events are generated by action event sources and handled by action event listeners. 13

14 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 Inner Classes F Non-static nested classes are called inner classes and are frequently used to implement event handlers. 14

15 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 Inner Classes 15

16 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 Event Handling F Before an application can respond to an event for a particular GUI component, you must perform several coding steps: 1. Create a class that represents the event handler. 2. Implement an appropriate interface, known as an event-listener interface, in the class. 3. Indicate that an object of the class should be notified when the event occurs. This is known as registering the event handler. 16

17 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 17 Selected Event Handlers Event ClassListener InterfaceListener Methods (Handlers) ActionEventActionListeneractionPerformed(ActionEvent) ItemEventItemListeneritemStateChanged(ItemEvent) WindowEventWindowListenerwindowClosing(WindowEvent) windowOpened(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowClosed(WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent) ContainerEventContainerListenercomponentAdded(ContainerEvent) componentRemoved(ContainerEvent) MouseEventMouseListenermousePressed(MouseEvent) mouseReleased(MouseEvent) mouseClicked(MouseEvent) mouseExited(MouseEvent) mouseEntered(MouseEvent) KeyEventKeyListenerkeyPressed(KeyEvent) keyReleased(KeyEvent) keyTypeed(KeyEvent)

18 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 18 java.awt.event.ActionEvent

19 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 19 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.

20 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 20 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. ShowInnerClass

21 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 21 Inner Classes, cont.

22 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 22 Inner Classes (cont.) F Inner classes can make programs simple and concise. F An inner class supports the work of its containing outer class.

23 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 23 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.

24 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 24 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. TestActionEvent Run

25 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 25 Interaction Between Source and Listener 1.jbtOK registers btListener by invoking addActionListener(btListner). 2.jbtCancel registers btListener by invoking addActionListener(btListner). 3.jbtOK invokes btListener ’ s actionPerformed method to process an ActionEvnet. 4.jbtCancel invokes btListener ’ s actionPerformed method to process an ActionEvent.

26 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 26 MouseEvent

27 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 27 Handling Mouse Events  Java provides two listener interfaces, MouseListener and MouseMotionListener, to handle mouse events.  The MouseListener listens for actions such as when the mouse is pressed, released, entered, exited, or clicked.  The MouseMotionListener listens for actions such as dragging or moving the mouse.

28 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 28 Handling Mouse Events

29 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 29 Example: Moving Message Using Mouse Objective: Create a program to display a message in a panel. You can use the mouse to move the message. The message moves as the mouse drags and is always displayed at the mouse point. MoveMessageDemoRun


Download ppt "Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 Chapter 14 Event-Driven Programming."

Similar presentations


Ads by Google