Interface EventTarget
- All Known Subinterfaces:
Attribute
,Document
,Element
,FormControlElement
,FormElement
,FrameElement
,ImageElement
,InputElement
,Node
,OptionElement
,SelectElement
,TextAreaElement
Provides methods to manage event listeners and dispatch events.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addEventListener
(EventType eventType, Observer<Event> listener, boolean useCapture) Adds the givenlistener
to the event target.boolean
Dispatches the givenevent
at the current event target.eventListeners
(EventType eventType, boolean useCapture) Returns the immutable list of event listeners that listen events of the giveneventType
in a phase that corresponds the givenuseCapture
.void
removeEventListener
(EventType eventType, Observer<Event> listener, boolean useCapture) Removes the givenlistener
from the event target.
-
Method Details
-
addEventListener
Adds the givenlistener
to the event target.You can use one of the predefined event types provided by the
EventType
class as aneventType
parameter or create it manually through theEventType.of(String)
, for example:EventType.of("click");
When an event of the giveneventType
is dispatched, theObserver.on(com.teamdev.jxbrowser.event.Event)
method will be invoked if one of the following statements is true:- the event is at the
EventPhase.AT_TARGET
phase - the method was invoked with the
useCapture
=true
and the event is at theEventPhase.CAPTURING_PHASE
phase - the method was invoked with the
useCapture
=false
and the event is at theEventPhase.BUBBLING_PHASE
phase
EventListener
instance for the same event type twice with the differentuseCapture
values, theEventListener#on(Object)
method will be invoked twice in theAT_TARGET
phase as the engine distinguishes these event listeners.This method does nothing if it has been already invoked with the same parameters.
- Parameters:
eventType
- the type of the event that the listener will listen tolistener
- the event listener instanceuseCapture
- a flag indicating that events of the giveneventType
will be dispatched to the givenlistener
before being dispatched to any otherEventTarget
beneath it in the DOM tree. Events that are bubbling upward through the tree will not trigger the listener- Throws:
ObjectClosedException
- when the document this instance belongs to is closed
- the event is at the
-
removeEventListener
Removes the givenlistener
from the event target.You can use one of the predefined event types provided by the
EventType
class as aneventType
parameter or create it manually through theEventType.of(String)
, for example:EventType.of("click");
If the event target has no added listeners of the giveneventType
, returns an empty collection.- Parameters:
eventType
- the type of the eventlistener
- the event listener instance to removeuseCapture
- a flag indicating that events of the giveneventType
will be dispatched to the givenlistener
before being dispatched to any otherEventTarget
beneath it in the DOM tree. Events that are bubbling upward through the tree will not trigger the listener- Throws:
ObjectClosedException
- when the document this instance belongs to is closed
-
eventListeners
Returns the immutable list of event listeners that listen events of the giveneventType
in a phase that corresponds the givenuseCapture
.You can use one of the predefined event types provided by the
EventType
class as aneventType
parameter or create it manually through theEventType.of(String)
, for example:EventType.of("click");
If the event target has no added listeners of this event type, returns an empty collection.- Parameters:
eventType
- the type of the event that the listener will listen touseCapture
- a flag indicating that events of the giveneventType
will be dispatched to the givenlistener
before being dispatched to any otherEventTarget
beneath it in the DOM tree. Events that are bubbling upward through the tree will not trigger the listener- Throws:
ObjectClosedException
- when the document this instance belongs to is closed
-
dispatch
Dispatches the givenevent
at the current event target. The event can be created using one of theDOMDocument.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);
- Parameters:
event
- the event to dispatch- Returns:
false
if the event is cancelable and at least one of the event listeners which handled this event called theEvent.preventDefault()
method. Otherwise returnstrue
.- Throws:
ObjectClosedException
- when the document this instance belongs to is closed
-