At any time, press the keyboard or mouse button to generate an event. Since JDK1.1, components have been unchanged in the way they accept and handle events. Swing components can generate many different categories of events, including those in the java.awt.event package, and in the javax.swing.event package. Those event categories from Swing are often associated with a specific SWING component. Each event category is an object. It indicates at least the event of the event, often with other messages, such as the category of this event, changes in the event source status before and after the event, and so on. Most of the event source is a normal component or model (MVC structure of the Swing component) in MODELS, MVC in the MVC. Other objects may also generate events.
To receive the notification generated, we need to register event listeners on the target object. The event listener is a specific implementation of any XXListener class or an interface (XX refer to the type of event). XXListener is a class or interface defined in java.awt.event, java.beans, and javax.swing.event packages. At least one method is defined in each interface, which is used as a parameter in the corresponding xxevent. Supporting a class that transmits the XXEvent event notification must implement the XXListener interface, and provide the corresponding addXXListener () and removexxListener () methods to register and remove these event listeners, most of the event target objects are allowed to register any more Event listener. A class that typically supports XXEvent provides protected methods () to construct event objects and send it to event handlers.
Javax.swing.event.eventListenerList class
EventListenerList is an array (array) containing XXEvent / XXListener pairs (PAIRS). JComponent and its derived class use an EventListenerList object to maintain its event listener. All default models (Models) also maintain event listeners and an EventListenerList. When a listener is registered to a Swing component or model (Model), the Class instance of the corresponding event (used to identify the event category) is added to the EventListenerList array, followed by the listener itself (ie, an XXEVENT / XXListener pair) ). Because these pairs are stored in arrays rather than in a variable collection (for efficiency), each addition and removal will call System.ArrayCopy () to generate a new array. When an event is received, the array is traveled, and the event will be sent to each event listener with its type. Since the array is arranged in the way XXEvent, XXListener, Yyevent, Yylistener, ..., a listener of an event class is always followed. This approach makes event processing very efficient. For thread safety, the method access array must be synchronized when the listener is added to the EventListenerList.
JComponent defines a protected EventListListener attribute called ListenerList, so all its subclasses have inherited this property. Swing components Manage most event listeners directly through the ListerList property.
Event send thread
The event listener accepts and handles the event in the event sending thread (an instance of a java.awt.eventdispatchthread class). All drawings and component layouts are also required to occur in this thread. Event Send Threads have headed importance in AWT and SWING, play a key role in terms of control component status and displayed at any time. Related to this thread is an event's FIFO (First In First Out, advanced first out) queue: system event queue (one instance of java.awt.eventqueue). Like all FIFO queues, the system event queue is also filled in linear. Each request runs the event processing code in turn, whether updating the component properties, layout, or re-drawing. All events are processed in order to avoid such a situation in which the status like a component is accidentally changed. After knowing this, we must avoid sending events outside the event to send threads. For example, call the firexx () method directly in another thread is not safe. We must also guarantee that the event handling code and drawing code are executed as soon as possible, otherwise the entire system queue will be blocked, and forced to wait for an event to be processed, redraw, or layout, and our app is like "Freezing" or loses response. ============================================================================================================================================================================================================= ===================================================================== // Thank matthew robinson And Pavel Vorobiev, Ph.d for their Great Book Swing, This Article is Translated from // This Book, 1st Edition. You Can Find these Text in Their Book (in english) of chapter 2. ======== ============================================================================================================================================================================================================= =====================================================================================================================================================