Java Event handling and sending

Source: Internet
Author: User
Tags array contains event listener implement interface key reference thread
At any time, pressing the keyboard or mouse button creates an event. The way the component accepts and handles events remains unchanged since JDK1.1. Swing components can produce many different categories of events, including those in the Java.awt.event package and in the Javax.swing.event package. The new event categories introduced by swing are often associated with specific swing components. Each event category is an object that at least indicates the source of the event, often with other messages, such as the category of the event, changes in the state of the event source before and after the event, and so on. The event source is mostly a generic component or model (models, or M in MVC, that might introduce the MVC architecture of swing components in future blogs). Other objects may also produce events.

To receive notifications from events, we need to register the event listener on the target object. An event listener is a specific implementation of an arbitrary Xxlistener class or interface (the type of the XX reference event). Xxlistener are classes or interfaces that are defined in the Java.awt.event, Java.beans, and javax.swing.event packages. At least one method is defined in each interface, with the corresponding xxevent as the parameter. Classes that support notification of sending Xxevent events must implement the Xxlistener interface and provide the corresponding Addxxlistener () and Removexxlistener () methods to register and remove these event listeners. Most event target objects (target) allow you to register any number of event listeners. Classes that typically support xxevent provide protected methods (protected method) Firexx () to construct an event object and to send it to the event handler for processing.


Javax.swing.event.EventListenerList class


Eventlistenerlist is an array (array) that contains xxevent/xxlistener pairs (pairs). JComponent and its derived classes use a Eventlistenerlist object to maintain their event listeners. All default models (models) also maintain event listeners and a eventlistenerlist. When a listener is registered to a swing component or 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 (that is, a xxevent/ Xxlistener right). Because these pairs are stored in an array rather than in a mutable set (mutable collection), each addition and removal will invoke system.arraycopy () to generate a new array. When an event is received, the array is traversed, and the event is sent to every event listener of its type. As the array is pressed xxevent, Xxlistener, Yyevent, Yylistener, ... , the listener for an event category is always followed. This approach makes event handling very efficient. For thread safety, the method access array must be synchronized when the listener is added and removed from the eventlistenerlist.

JComponent defines a protected Eventlistlistener property named ListenerList, so all its subclasses inherit this property. The swing component manages most of its event listeners directly through the ListenerList property.


Event Send Thread


The event listener accepts and handles events in an event-sending thread (an instance of a Java.awt.EventDispatchThread class). All drawing and assembly layouts are also required to occur in this thread. Event-Sending threads are of paramount importance in both AWT and swing, and play a key role in controlling component status and displaying updates at all times in applications.

Associated with this thread is the FIFO of an event (first-out, FIFO) Queue: System event Queue (an instance of Java.awt.EventQueue). As with all FIFO queues, the system event queues are also linearly populated. Whether you are updating component properties, layouts, or redrawing, each request runs the event handling code sequentially. All events are processed sequentially to avoid situations where the state of a component is accidentally changed halfway through its redrawing. Knowing this, we need to avoid sending events outside the thread threads. For example, it is not safe to call the Firexx () method directly in another thread. We also have to make sure that the event handling code and the drawing code are done as quickly as possible, otherwise the entire system queue is blocked, forced to wait for an event to be processed, redrawn, or laid out, and our application is "Frozen" or unresponsive.
==============================================================================================
Matthew Robinson and Pavel Vorobiev, Ph.D for their great book Swing, this article is translated form
This book, 1st edition. Can find this text in their book (in 中文版) of Chapter 2.
=================================================================================



To receive notifications from events, we need to register the event listener on the target object. An event listener is a specific implementation of an arbitrary Xxlistener class or interface (the type of the XX reference event). Xxlistener are classes or interfaces that are defined in the Java.awt.event, Java.beans, and javax.swing.event packages. At least one method is defined in each interface, with the corresponding xxevent as the parameter. Classes that support notification of sending Xxevent events must implement the Xxlistener interface and provide the corresponding Addxxlistener () and Removexxlistener () methods to register and remove these event listeners. Most event target objects (target) allow you to register any number of event listeners. Classes that typically support xxevent provide protected methods (protected method) Firexx () to construct an event object and to send it to the event handler for processing.


Javax.swing.event.EventListenerList class


Eventlistenerlist is an array (array) that contains xxevent/xxlistener pairs (pairs). JComponent and its derived classes use a Eventlistenerlist object to maintain their event listeners. All default models (models) also maintain event listeners and a eventlistenerlist. When a listener is registered to a swing component or 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 (that is, a xxevent/ Xxlistener right). Because these pairs are stored in an array rather than in a mutable set (mutable collection), each addition and removal will invoke system.arraycopy () to generate a new array. When an event is received, the array is traversed, and the event is sent to every event listener of its type. As the array is pressed xxevent, Xxlistener, Yyevent, Yylistener, ... , the listener for an event category is always followed. This approach makes event handling very efficient. For thread safety, the method access array must be synchronized when the listener is added and removed from the eventlistenerlist.

JComponent defines a protected Eventlistlistener property named ListenerList, so all its subclasses inherit this property. The swing component manages most of its event listeners directly through the ListenerList property.


Event Send Thread


The event listener accepts and handles events in an event-sending thread (an instance of a Java.awt.EventDispatchThread class). All drawing and assembly layouts are also required to occur in this thread. Event-Sending threads are of paramount importance in both AWT and swing, and play a key role in controlling component status and displaying updates at all times in applications.

Associated with this thread is the FIFO of an event (first-out, FIFO) Queue: System event Queue (an instance of Java.awt.EventQueue). As with all FIFO queues, the system event queues are also linearly populated. Whether you are updating component properties, layouts, or redrawing, each request runs the event handling code sequentially. All events are processed sequentially to avoid situations where the state of a component is accidentally changed halfway through its redrawing. Knowing this, we need to avoid sending events outside the thread threads. For example, it is not safe to call the Firexx () method directly in another thread. We also have to make sure that the event handling code and the drawing code are done as quickly as possible, otherwise the entire system queue is blocked, forced to wait for an event to be processed, redrawn, or laid out, and our application is "Frozen" or unresponsive.
==============================================================================================
Matthew Robinson and Pavel Vorobiev, Ph.D for their great book Swing, this article is translated form
This book, 1st edition. Can find this text in their book (in 中文版) of Chapter 2.
=================================================================================



To receive notifications from events, we need to register the event listener on the target object. An event listener is a specific implementation of an arbitrary Xxlistener class or interface (the type of the XX reference event). Xxlistener are classes or interfaces that are defined in the Java.awt.event, Java.beans, and javax.swing.event packages. At least one method is defined in each interface, with the corresponding xxevent as the parameter. Classes that support notification of sending Xxevent events must implement the Xxlistener interface and provide the corresponding Addxxlistener () and Removexxlistener () methods to register and remove these event listeners. Most event target objects (target) allow you to register any number of event listeners. Classes that typically support xxevent provide protected methods (protected method) Firexx () to construct an event object and to send it to the event handler for processing.


Javax.swing.event.EventListenerList class


Eventlistenerlist is an array (array) that contains xxevent/xxlistener pairs (pairs). JComponent and its derived classes use a Eventlistenerlist object to maintain their event listeners. All default models (models) also maintain event listeners and a eventlistenerlist. When a listener is registered to a swing component or 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 (that is, a xxevent/ Xxlistener right). Because these pairs are stored in an array rather than in a mutable set (mutable collection), each addition and removal will invoke system.arraycopy () to generate a new array. When an event is received, the array is traversed, and the event is sent to every event listener of its type. As the array is pressed xxevent, Xxlistener, Yyevent, Yylistener, ... , the listener for an event category is always followed. This approach makes event handling very efficient. For thread safety, the method access array must be synchronized when the listener is added and removed from the eventlistenerlist.

JComponent defines a protected Eventlistlistener property named ListenerList, so all its subclasses inherit this property. The swing component manages most of its event listeners directly through the ListenerList property.


Event Send Thread


The event listener accepts and handles events in an event-sending thread (an instance of a Java.awt.EventDispatchThread class). All drawing and assembly layouts are also required to occur in this thread. Event-Sending threads are of paramount importance in both AWT and swing, and play a key role in controlling component status and displaying updates at all times in applications.

Associated with this thread is the FIFO of an event (first-out, FIFO) Queue: System event Queue (an instance of Java.awt.EventQueue). As with all FIFO queues, the system event queues are also linearly populated. Whether you are updating component properties, layouts, or redrawing, each request runs the event handling code sequentially. All events are processed sequentially to avoid situations where the state of a component is accidentally changed halfway through its redrawing. Knowing this, we need to avoid sending events outside the thread threads. For example, it is not safe to call the Firexx () method directly in another thread. We also have to make sure that the event handling code and the drawing code are done as quickly as possible, otherwise the entire system queue is blocked, forced to wait for an event to be processed, redrawn, or laid out, and our application is "Frozen" or unresponsive.
==============================================================================================
Matthew Robinson and Pavel Vorobiev, Ph.D for their great book Swing, this article is translated form
This book, 1st edition. Can find this text in their book (in 中文版) of Chapter 2.
=================================================================================


JDK1.1 have remained unchanged ever since. Swing components can produce many different categories of events, including those in the Java.awt.event package and in the Javax.swing.event package. The new event categories introduced by swing are often associated with specific swing components. Each event category is an object that at least indicates the source of the event, often with other messages, such as the category of the event, changes in the state of the event source before and after the event, and so on. The event source is mostly a generic component or model (models, or M in MVC, that might introduce the MVC architecture of swing components in future blogs). Other objects may also produce events.

To receive notifications from events, we need to register the event listener on the target object. An event listener is a specific implementation of an arbitrary Xxlistener class or interface (the type of the XX reference event). Xxlistener are classes or interfaces that are defined in the Java.awt.event, Java.beans, and javax.swing.event packages. At least one method is defined in each interface, with the corresponding xxevent as the parameter. Classes that support notification of sending Xxevent events must implement the Xxlistener interface and provide the corresponding Addxxlistener () and Removexxlistener () methods to register and remove these event listeners. Most event target objects (target) allow you to register any number of event listeners. Classes that typically support xxevent provide protected methods (protected method) Firexx () to construct an event object and to send it to the event handler for processing.


Javax.swing.event.EventListenerList class


Eventlistenerlist is an array (array) that contains xxevent/xxlistener pairs (pairs). JComponent and its derived classes use a Eventlistenerlist object to maintain their event listeners. All default models (models) also maintain event listeners and a eventlistenerlist. When a listener is registered to a swing component or 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 (that is, a xxevent/ Xxlistener right). Because these pairs are stored in an array rather than in a mutable set (mutable collection), each addition and removal will invoke system.arraycopy () to generate a new array. When an event is received, the array is traversed, and the event is sent to every event listener of its type. As the array is pressed xxevent, Xxlistener, Yyevent, Yylistener, ... , the listener for an event category is always followed. This approach makes event handling very efficient. For thread safety, the method access array must be synchronized when the listener is added and removed from the eventlistenerlist.

JComponent defines a protected Eventlistlistener property named ListenerList, so all its subclasses inherit this property. The swing component manages most of its event listeners directly through the ListenerList property.


Event Send Thread


The event listener accepts and handles events in an event-sending thread (an instance of a Java.awt.EventDispatchThread class). All drawing and assembly layouts are also required to occur in this thread. Event-Sending threads are of paramount importance in both AWT and swing, and play a key role in controlling component status and displaying updates at all times in applications.

Associated with this thread is the FIFO of an event (first-out, FIFO) Queue: System event Queue (an instance of Java.awt.EventQueue). As with all FIFO queues, the system event queues are also linearly populated. Whether you are updating component properties, layouts, or redrawing, each request runs the event handling code sequentially. All events are processed sequentially to avoid situations where the state of a component is accidentally changed halfway through its redrawing. Knowing this, we need to avoid sending events outside the thread threads. For example, it is not safe to call the Firexx () method directly in another thread. We also have to make sure that the event handling code and the drawing code are done as quickly as possible, otherwise the entire system queue is blocked, forced to wait for an event to be processed, redrawn, or laid out, and our application is "Frozen" or unresponsive.
==============================================================================================
Matthew Robinson and Pavel Vorobiev, Ph.D for their great book Swing, this article is translated form
This book, 1st edition. You can find this text in their book (in 中文版) in Chapter 2.
=================================================================================


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.