1 / 8

Event-Driven Programming

Event-Driven Programming. Event: A signal to the program that something has happened. It can be triggered either by external user actions, such as mouse movements, button clicks, and keystrokes, or by the operating system, such as a timer.

huyen
Download Presentation

Event-Driven Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Event-Driven Programming • Event: • A signal to the program that something has happened. • It can be triggered either by external user actions, such as mouse movements, button clicks, and keystrokes, or by the operating system, such as a timer. • The program can choose to respond to or ignore an event. • Source Object: • The component on which an event is generated is called the source object. • button, button click

  2. User Action, Source Object and Event type

  3. A simple example • Write a program that display two buttons. OK and Cancel, in a window. A message is displayed on the console to indicate which button and when it is clicked

  4. Listeners, Registrations, and Handling Events • An external user action on a source object triggers an event, and an listener receives the event. • The listener must implement the corresponding event-listener interface • The listener must be registered by the source object

  5. Listener interface • Java provides a listener interface for every type of GUI event. • The listener interface is usually names XListener for XEvent, with excpetion of MouseMotionListener • For example, listener interface for ActionEvent is ActionListener • Each listener for ActionEvent should implement the ActionListener interface • The listener interface contains the methods, know as the handlers, which process the events

  6. Events, Event Listeners, and Listener Methods

  7. Registration • The listener object must be registered by the source object. Registration methods are dependent on the event type. For the ActionEvent, the method is addActionListener • In general, the method is named addXListener for the XEvent • ListenerClass lis = new ListenerClass(); • JButton jbt – new JButton(“OK”); • Jbt.addActionListener(lis);

  8. Handling the Event • When you click the button, the JButton object generates an ActionEvent and passes it to invoke the actionPerformed method to handle the event • The event object contains information pertinent to the event type

More Related