In How to Use Color Choosers , for instance, you will find an example of writing a change listener to track when the color changes in the color chooser.
The following example demonstrates that event listeners can be registered on multiple objects and that the same event can be sent to multiple listeners. The example contains two event sources JButton instances and two event listeners. One of the event listeners an instance of a class called MultiListener listens for events from both buttons. When it receives an event, it adds the event's "action command" which is set to the text on the button's label to the top text area. The second event listener an instance of a class called Eavesdropper listens for events on only one of the buttons.
When it receives an event, it adds the action command to the bottom text area. You can find the entire program in MultiListener. In the above code, both MultiListener and Eavesdropper implement the ActionListener interface and register as action listeners using the JButton addActionListener method.
Both classes' implementations of the actionPerformed method are similar: they simply add the event's action command to a text area. All rights reserved.
Hide TOC. The benefit of this approach is that the user interface logic is completely separated from the logic that generates the event. The user interface element is able to delegate the processing of an event to the separate piece of code. In this model ,Listener needs to be registered with the source object so that the listener can receive the event notification.
This is an efficient way of handling the event because the event notifications are sent only to those listener that want to receive them. Now the object of concerned event class is created automatically and information about the source and the event get populated with in same object.
In order to design a listener class we have to develop some listener interfaces. These Listener interfaces forecast some public abstract callback methods which must be implemented by the listener class. If you do not implement the any if the predefined interfaces then your class can not act as a listener class for a source object.
These are the methods that are provided by API provider and are defined by the application programmer and invoked by the application developer.
Here the callback methods represents an event method. Here are some examples:. More generally, an event is a state change at an instant in time Winston. The word "event" also refers to an instance of the EventObject class, or a subclass of that class. Internally, the component may store a listener however it chooses; usually, however, components store listeners in a java.
Vector or javax. To fire off an event to its listeners, the component simply loops through its list of listeners and passes the event to each listener by calling the listener's event dispatch method. This example shows how to register, deregister, and fire events of type XXXEvent.
When an event occurs, the component creates an event object and passes it to the fireXXX method, where it is passed to the listeners. The example defines a generic recipe that all components can follow. The event holds all of the information necessary for a listener to figure out what happened. The information included is really event specific. Just think about the event carefully and design the event class to hold whatever information is necessary to fully describe the event to a listener. Events normally extend the java.
AWTEvent event class. An event listener interface defines the methods used by a component to dispatch events. Each event type will have at least one corresponding dispatch method in a listener interface. To listen for an event, a listener must implement the XXXListener interface and register itself with the component.
0コメント