Overview of SWT event model and common event processing methods

Source: Internet
Author: User

1. listener (listener)
The so-called listener, that is, the object that receives the event, is responsible for processing the code that responds to the event after the event arrives. The listener object is an interface. To respond to an event, you must implement the methods in this interface. The listener class is in the form of xxxlistener. For example, commonly used listeners include selectionlistener, mouselistener, and keylistener.
A listener has different methods to respond to events. For example, in selectionlistener, You need to implement the widgetselected two methods: widgetdefaselecselected. Widgetselected indicates the selected event. For the list, click a method called in the list. But for the button, it is the method called by clicking this button. Different controls have different effects on Event Publishing. The widgetdefaselecselected event is the method called by double-clicking an event in the list. Therefore, the listener is also selected, and the events triggered by clicking and double-clicking are different. Therefore, we need to distinguish the method in the listener from the type of events that the listener responds.

2. Events)
When an event arrives at the event listening object, it carries some information attached to the event. For example, the time when an event occurs, and the controls by which the event occurs. The information is expressed in xxxevent format. For example:
Public void widgetselected (selectionevent e ){
List I = (list) E. widget;
}
The event corresponding to mouselistener is mouseevent, and the event corresponding to the keyboard listener is keyevent. The information carried by each event is slightly different. However, the following information can be obtained from any event:
◆ E. display: Display object when an event occurs
◆ E. Data: Save the data used by the system, which is generally not commonly used.
◆ E. Widget: control object in which an event occurs. A forced type conversion is usually required because all controls inherit from the widget class.
◆ E. Time: the time when the event occurred.
This is because all xxxevent classes inherit from the typedevent class. The attributes in the typedevent class are the ones listed above. For a specific type of event, the information of the carried type is also slightly different.

3. register the listener
Register the listener: addxxxlistener (...)
Remove listener: removexxxlistener (...)

4. Adapter
Xxxadapter. The adapter simplifies the code for event processing.
List. addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent e ){
}
});
After the adapter class is used, the method in the parent class can be overwritten to achieve the same effect as the listener interface. The principle is simple. The adapter class is only an abstract class that implements the listener interface. The source code of the selectionadapter class corresponding to selectionlistener is as follows:
Public abstract class selectionadapter implements selectionlistener {
Public void widgetselected (selectionevent e ){}
Public void widgetdefaselecselected (selectionevent e ){}
}
Not every listener corresponds to an adapter class. It is necessary to use the listener class only when there are multiple methods to respond to events in the listener class. The listener implementation depends on the method of selecting the adapter class or implementing the interface. To sum up, there are several steps for the control to respond to events:
(1) create a listener object and implement the method (xxxlistener) in the listener ).
(2) register the listener (addxxxlistener) for the control ).
(3) the bridge between the control and the listener is the event object (xxxevent ).
(4) If there are multiple event response methods in the listener interface, you can also use the corresponding adapter (xxxadapter) to create the listener object by overwriting the methods in the parent class.

5. Common events
Reference: common Java. SWT: SWT events

8.2 common event processing methods

1. Internal Anonymous class
It is suitable for processing simple events. As follows:
List. addselectionlistener (New selectionlistener (){
// Program code
});

2. Internal class
Selectionlistener listener = new selectionlistener (){
Public void widgetselected (selectionevent e ){
}
Public void widgetdefaselecselected (selectionevent e ){
}
};
List. addselectionlistener (listener );
List. removeselectionlistener (listener );

3. Classes implementing Interfaces
The listener object is essentially an interface and can be processed using the interface's general method. As follows:
Public class mylistener implements selectionlistener {
Public void widgetselected (selectionevent e ){
}
Public void widgetdefaselecselected (selectionevent e ){
}
}
The code for registering the listener object is as follows:
Button button = new button (shell, SWT. None );
Button. addselectionlistener (New mylistener ());

The internal class method is applicable to handling various events in a class. In actual projects, all events are usually concentrated in a class, in this way, the code for processing the event can be reused in other classes, so this method is often used in projects.

4. Methods of inherited classes
For the corresponding adapter class, you can also inherit the adapter class and then override the method in the adapter class. For example:
Public class myadapter extends selectionadapter {
Public void widgetselected (selectionevent e ){
}
Public void widgetdefaselecselected (selectionevent e ){
}
}

Button button = new button (shell, SWT. None );
Button. addselectionlistener (New myadapter ());

The Inheritance Method ensures that the class does not inherit from other classes. If the class inherits from other classes, you can only use the method that implements the interface. If the class does not inherit from other classes, both methods can be used.
In general, methods that implement interfaces are more universal, but inherited methods have certain limitations, while inherited methods simplify the amount of code.

 

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.