Event Firing"
(Created page with '{{ZKComponentReferencePageHeader}} = Overview = Events are usually fired (aka., triggered) by a component (when serving the user at the client). However, applications are allow…') |
|||
Line 22: | Line 22: | ||
= Send an Event = | = Send an Event = | ||
+ | |||
+ | If you prefer to trigger an event to a component directly and process it immediately without going thru the event queue, you could use <javadoc method="sendEvent(java.lang.String, org.zkoss.zk.ui.Component, java.lang.Object)">org.zkoss.zk.ui.event.Events</javadoc> to trigger the event. | ||
+ | |||
+ | <source lang="java"> | ||
+ | Events.sendEvent("onMyEvent", component, mydata); | ||
+ | </source> | ||
+ | |||
+ | Notice that the <tt>sendEvent</tt> method won't return until all handlers and listeners registered for this event has been processed. Also notice that the event handlers and listeners are invoked directly without starting any event thread (even if the event thread is enabled). | ||
= Echo an Event = | = Echo an Event = | ||
{{ZKComponentReferencePageFooter}} | {{ZKComponentReferencePageFooter}} |
Revision as of 08:10, 24 August 2010
Overview
Events are usually fired (aka., triggered) by a component (when serving the user at the client). However, applications are allowed to send events too.
There are three ways to trigger an event: post, send and echo.
Post an Event
Posting is the most common way to trigger an event. By posting, the event is placed at the end of the event queue, while events stored in the event queue is processed one-by-one in first-in-first-out order. Each desktop has one event queue and all events are handled sequentially.
To trigger an event, you could invoke Events.postEvent(String, Component, Object). For example,
Events.postEvent("onClick", button, null); //simulate a click
In additions to post an event to the end of the event queue, you could specify a priority with Events.postEvent(int, String, Component, Object). By default, the priority is 0. The higher the priority the earlier an event is processed.
Notice that the invocation returns after placing the event to the event queue. In other words, the event won't be processed unless all other events posted earlier or with higher priority are processed.
Send an Event
If you prefer to trigger an event to a component directly and process it immediately without going thru the event queue, you could use Events.sendEvent(String, Component, Object) to trigger the event.
Events.sendEvent("onMyEvent", component, mydata);
Notice that the sendEvent method won't return until all handlers and listeners registered for this event has been processed. Also notice that the event handlers and listeners are invoked directly without starting any event thread (even if the event thread is enabled).
Echo an Event