Interface EventTarget

All Known Subinterfaces:
Attribute, Document, Element, FormControlElement, FormElement, FrameElement, ImageElement, InputElement, Node, OptionElement, SelectElement, TextAreaElement

public interface EventTarget
Represents an event target that is able to process events, for example, a DOM node.

Provides methods to manage event listeners and dispatch events.

  • Method Summary

    Modifier and Type
    Method
    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.
    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.
  • Method Details

    • addEventListener

      void addEventListener(EventType eventType, Observer<Event> listener, boolean useCapture)
      Adds the given 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: Please note, that if you add the same 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.

      Parameters:
      eventType - the type of the event that the listener will listen to
      listener - the event listener instance
      useCapture - 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 listener
      Throws:
      ObjectClosedException - when the document this instance belongs to is closed
    • removeEventListener

      void removeEventListener(EventType eventType, Observer<Event> listener, boolean useCapture)
      Removes the given 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.
      Parameters:
      eventType - the type of the event
      listener - the event listener instance to remove
      useCapture - 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 listener
      Throws:
      ObjectClosedException - when the document this instance belongs to is closed
    • eventListeners

      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.

      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.
      Parameters:
      eventType - the type of the event that the listener will listen to
      useCapture - 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 listener
      Throws:
      ObjectClosedException - when the document this instance belongs to is closed
    • dispatch

      boolean dispatch(Event event)
      Dispatches the given 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);
       
      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 the Event.preventDefault() method. Otherwise returns true.
      Throws:
      ObjectClosedException - when the document this instance belongs to is closed