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 TypeMethodDescriptionvoidaddEventListener(EventType eventType, Observer<Event> listener, boolean useCapture) Adds the givenlistenerto the event target.booleanDispatches the giveneventat the current event target.eventListeners(EventType eventType, boolean useCapture) Returns the immutable list of event listeners that listen events of the giveneventTypein a phase that corresponds the givenuseCapture.voidremoveEventListener(EventType eventType, Observer<Event> listener, boolean useCapture) Removes the givenlistenerfrom the event target.
-
Method Details
-
addEventListener
Adds the givenlistenerto the event target.You can use one of the predefined event types provided by the
EventTypeclass as aneventTypeparameter or create it manually through theEventType.of(String), for example:EventType.of("click");When an event of the giveneventTypeis 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_TARGETphase - the method was invoked with the
useCapture=trueand the event is at theEventPhase.CAPTURING_PHASEphase - the method was invoked with the
useCapture=falseand the event is at theEventPhase.BUBBLING_PHASEphase
EventListenerinstance for the same event type twice with the differentuseCapturevalues, theEventListener#on(Object)method will be invoked twice in theAT_TARGETphase 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 giveneventTypewill be dispatched to the givenlistenerbefore being dispatched to any otherEventTargetbeneath 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 givenlistenerfrom the event target.You can use one of the predefined event types provided by the
EventTypeclass as aneventTypeparameter 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 giveneventTypewill be dispatched to the givenlistenerbefore being dispatched to any otherEventTargetbeneath 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 giveneventTypein a phase that corresponds the givenuseCapture.You can use one of the predefined event types provided by the
EventTypeclass as aneventTypeparameter 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 giveneventTypewill be dispatched to the givenlistenerbefore being dispatched to any otherEventTargetbeneath 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 giveneventat 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
eventTargetyou 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:
falseif 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
-