public interface EventTarget
Provides methods to manage event listeners and dispatch events.
Modifier and Type | Method and Description |
---|---|
void |
addEventListener(EventType eventType,
Observer<Event> listener,
boolean useCapture)
Adds the given
listener to the event target. |
boolean |
dispatch(Event event)
Dispatches the given
event at the current event target. |
java.util.List<Observer<Event>> |
eventListeners(EventType eventType,
boolean useCapture)
Returns the immutable list of event listeners that listen events of the given
eventType in a phase that corresponds the given useCapture . |
void |
removeEventListener(EventType eventType,
Observer<Event> listener,
boolean useCapture)
Removes the given
listener from the event target. |
void addEventListener(EventType eventType, Observer<Event> listener, boolean useCapture)
listener
to the event target.
You can use one of the predefined event types provided by the EventType
class as
an eventType
parameter or create it manually through the EventType.of(String)
, for example:
EventType.of("click");When an event of the given
eventType
is dispatched, the Observer.on(com.teamdev.jxbrowser.event.Event)
method will be invoked if one of the
following statements is true:
EventPhase.AT_TARGET
phase
useCapture
= true
and the event is at
the EventPhase.CAPTURING_PHASE
phase
useCapture
= false
and the event is at
the EventPhase.BUBBLING_PHASE
phase
EventListener
instance for the same event type
twice with the different useCapture
values, the EventListener#on(Object)
method will be invoked twice in the AT_TARGET
phase as the engine distinguishes these
event listeners.
This method does nothing if it has been already invoked with the same parameters.
eventType
- the type of the event that the listener will listen tolistener
- the event listener instanceuseCapture
- a flag indicating that events of the given eventType
will be
dispatched to the given listener
before being dispatched to any
other EventTarget
beneath it in the DOM tree. Events that are
bubbling upward through the tree will not trigger the listenerjava.lang.IllegalStateException
- when the document this instance belongs to is closedvoid removeEventListener(EventType eventType, Observer<Event> listener, boolean useCapture)
listener
from the event target.
You can use one of the predefined event types provided by the EventType
class as
an eventType
parameter or create it manually through the EventType.of(String)
, for example:
EventType.of("click");If the event target has no added listeners of the given
eventType
, returns an empty
collection.eventType
- the type of the eventlistener
- the event listener instance to removeuseCapture
- a flag indicating that events of the given eventType
will be
dispatched to the given listener
before being dispatched to any
other EventTarget
beneath it in the DOM tree. Events that are
bubbling upward through the tree will not trigger the listenerjava.lang.IllegalStateException
- when the document this instance belongs to is closedjava.util.List<Observer<Event>> eventListeners(EventType eventType, boolean useCapture)
eventType
in a phase that corresponds the given useCapture
.
You can use one of the predefined event types provided by the EventType
class as
an eventType
parameter or create it manually through the EventType.of(String)
, for example:
EventType.of("click");If the event target has no added listeners of this event type, returns an empty collection.
eventType
- the type of the event that the listener will listen touseCapture
- a flag indicating that events of the given eventType
will be
dispatched to the given listener
before being dispatched to any
other EventTarget
beneath it in the DOM tree. Events that are
bubbling upward through the tree will not trigger the listenerjava.lang.IllegalStateException
- when the document this instance belongs to is closedboolean dispatch(Event event)
event
at the current event target. The event can be created
using one of the DOMDocument.create*Event()
methods.
For example, to perform a single mouse click event by a primary mouse button on an eventTarget
you can use the following code:
Point location = Point.of(100, 200); MouseEventParams eventParams = MouseEventParams.newBuilder() .button(Button.MAIN) .clientLocation(location) .screenLocation(location) .uiEventModifierParams(UiEventModifierParams.newBuilder() .eventParams(EventParams.newBuilder() .bubbles(true) .cancelable(true) .build()) .build()) .build(); MouseEvent mouseEvent = document.createMouseEvent( EventType.CLICK, eventParams); eventTarget.dispatch(mouseEvent);
event
- the event to dispatchfalse
if the event is cancelable and at least one of the event listeners
which handled this event called the Event.preventDefault()
method. Otherwise returns
true
.java.lang.IllegalStateException
- when the document this instance belongs to is closed