1 / 28

Programming in Java Event Handling

Programming in Java Event Handling. Lovely Professional University, Punjab. Outlines. Delegation Event Model ActionListener ItemListener KeyListener MouseListener MouseMotionListener. Introduction. An event can be defined as a signal to the program that something has happened.

casanova
Download Presentation

Programming in Java Event Handling

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. Programming in JavaEvent Handling Lovely Professional University, Punjab

  2. Outlines • Delegation Event Model • ActionListener • ItemListener • KeyListener • MouseListener • MouseMotionListener

  3. Introduction • An event can be defined as a signal to the program that something has happened. • Events are triggered either by external user actions, such as mouse movements, button clicks, and keystrokes, or by internal program activities, such as a timer. • The program can choose to respond to or ignore an event. • The component that creates an event and fires it is called the source object or source component. • For example, a button is the source object for a button-clicking action event.

  4. Introduction • An event is an instance of an event class. • The root class of the event classes isjava.util.EventObject. • We can identify the source object of an event using the getSource() method in the EventObject class. • The subclasses of EventObject deal with special types of events, such as action events, window events, component events, mouse events, and key events.

  5. Delegation Event Model

  6. The Delegation Event Model • The delegation event model defines standard and consistent mechanisms to generate and process events. Principle: • A source generates an event and sends it to one or more listeners. • The listener waits until it receives an event. • Once an event is received, the listener processes the event and then returns. Advantage: • The application logic that processes events is cleanly separated from the user interface logic that generates those events. • A user interface element is able to “delegate” the processing of an event to a separate piece of code.

  7. In the delegation event model, listeners must register with a source in order to receive an event notification. • This provides an important benefit: notifications are sent only to listeners that want to receive them.

  8. Event • An event is an object that describes a state change in a source. • It can be generated as a consequence of a person interacting with the elements in a graphical user interface. • For Example, pressing a button, entering a character via the keyboard, selecting an item in a list, and clicking the mouse. • Events may also occur that are not directly caused by interactions with a user interface. • For example, an event may be generated when a timer expires, a counter exceeds a value, a software or hardware failure occurs, or an operation is completed.

  9. Event Source • An Event source is an object that generates an event. • This occurs when the internal state of that object changes in some way. • Sources may generate more than one type of event. • A source must register listeners in order for the listeners to receive notifications about a specific type of event. • Each type of event has its own registration method. public void addTypeListener(TypeListener el)

  10. When an event occurs, all registered listeners are notified and receive a copy of the event object. This is known as multicasting the event. • In all cases, notifications are sent only to listeners that register to receive them. • Some sources may allow only one listener to register. public void addTypeListener(TypeListener el) throws java.util.TooManyListenersException

  11. Event Listener • A listener is an object that is notified when an event occurs. It has two major requirements. • First, it must have been registered with one or more sources to receive notifications about specific types of events. • Second, it must implement methods to receive and process these notifications. • The methods that receive and process events are defined in a set of interfaces found in java.awt.event. • For example, the MouseMotionListener interface defines two methods to receive notifications when the mouse is dragged or moved.

  12. Listener Interfaces

  13. Listener API Table

  14. ActionListener • Action listeners are most common event handlers to implement. • An action event occurs, whenever an action is performed by the user. • We implement an action listener to define what should be done when an user performs certain operation. Examples: When the user clicks a button, chooses a menu item, presses Enter in a text field. • The result is that an actionPerformed message is sent to all action listeners that are registered on the relevant component.

  15. To write an Action Listener, follow the steps given below: • Declare an event handler class and specify that the class either implements an ActionListener interface or extends a class that implements an ActionListener interface. For example: public class MyClass implements ActionListener { • Register an instance of the event handler class as a listener on one or more components. For example: someComponent.addActionListener(instanceOfMyClass); • Include code that implements the methods in listener interface. For example: public void actionPerformed(ActionEvent e) { ...//code that reacts to the action... }

  16. ActionEvent Class

  17. ItemListener Interface • Item events are fired by components that implement the ItemSelectable interface. • Generally, ItemSelectable components maintain on/off state for one or more items. • The Swing components that fire item events include buttons like check boxes, check menu items, toggle buttons and combo boxes etc. • ItemListener Interface has only one method. public void itemStateChanged (ItemEvent)

  18. ItemEvent class

  19. KeyListener Interface • Key events indicate when the user is typing at the keyboard. • Key events are fired by the component with the keyboard focus when the user presses or releases keyboard keys. • Notifications are sent about two basic kinds of key events: • The typing of a Unicode character • The pressing or releasing of a key on the keyboard

  20. The first kind of event is called a key-typedevent. • To know when the user types a Unicode character ? whether by pressing one key such as 'a' or by pressing several keys in sequence ? • The second kind is either a key-pressed or key-releasedevent. • To know when the user presses the F1 key, or whether the user pressed the '3' key on the number pad, you handle key-pressed events.

  21. Methods of KeyListener Interface

  22. KeyEvent class

  23. MouseListener Interface • Mouse events notify when the user uses the mouse (or similar input device) to interact with a component. • Mouse events occur when the cursor enters or exits a component's onscreen area and when the user presses or releases one of the mouse buttons.

  24. Methods of MouseListener Interface

  25. MouseEvent class

  26. MouseAdapter Class • MouseAdapter class provides an empty implementation of all the methods in MouseListener interface. This class exists as convenience for creating listener objects. • Extend this class to create a MouseEvent listener and override the methods for the events of interest. • Create a listener object using the extended class and then register it with a component using the component's addMouseListener method. • When a mouse button is pressed, released, or clicked (pressed and released), or when the mouse cursor enters or exits the component, the relevant method in the listener object is invoked and the MouseEvent is passed to it.

  27. MouseMotionListener Interface • Mouse-motion events notify when the user uses the mouse (or a similar input device) to move the onscreen cursor. • If an application requires the detection of both mouse events and mouse-motion events, use the MouseInputAdapter class. • It implements the MouseInputListener a convenient interface that implements both the MouseListener and MouseMotionListener interfaces.

  28. Methods of MouseMotionListener Interface

More Related