Widget:notifyListeners方法---主动触发事件

时间:2021-10-21 22:20:19

这里把去点击某个小部件(widget)所导致的事件的过程当作是被动的。因为事件触发是被动的。

主动是指在其它部分的代码里去触发小部件所注册的事件,而本身并没有真正意义上的去点击控件。

 

使用示例

Event event = new Event();
event.type = SWT.Selection;
b1.notifyListeners(SWT.Selection, event);

 

方法描述和原型:

/**
 * Notifies all of the receiver's listeners for events
 * of the given type that one such event has occurred by
 * invoking their <code>handleEvent()</code> method.  The
 * event type is one of the event constants defined in class
 * <code>SWT</code>.
 *
 * @param eventType the type of event which has occurred
 * @param event the event data
 *
 * @exception SWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 *
 * @see SWT
 * @see #addListener
 * @see #getListeners(int)
 * @see #removeListener(int, Listener)
 */
public void notifyListeners (int eventType, Event event) {
 checkWidget();
 if (event == null) event = new Event ();
 sendEvent (eventType, event);
}