Package swingtree

Class ComponentMouseEventDelegate<C extends JComponent>

Direct Known Subclasses:
ComponentDragEventDelegate, TabDelegate

public class ComponentMouseEventDelegate<C extends JComponent> extends ComponentDelegate<C,MouseEvent>
A JComponent as well as MouseEvent delegate providing useful context information to various Action listeners which are typically registered through UIForAnySwing.onMouseClick(sprouts.Action)}, UIForAnySwing.onMousePress(sprouts.Action) and UIForAnySwing.onMouseRelease(sprouts.Action), among others.
This delegate is designed to provide clutter-free access to both the concrete JComponent subtype C as well a the MouseEvent which triggered the action.

Here is an example of how this delegate is typically exposed in your Action:


  UI.panel()
  .onMouseMove( it -> {
    System.out.println("Moved on " + it.get());
    System.out.println("Moved at " + it.mouseX() + ", " + it.mouseY());
  })
  .onMouseClick( it -> {
    System.out.println("Clicked on " + it.get());
    System.out.println("Click count: " + it.clickCount());
  })
  //...
  

For some more examples please take a look at the living swing-tree documentation.

  • Method Details

    • isLeftMouseButton

      public final boolean isLeftMouseButton()
      Returns true if the mouse event of this delegate is the left mouse button.
      Returns:
      true if the left mouse button was active
    • isRightMouseButton

      public final boolean isRightMouseButton()
      Returns true if the mouse event of this delegate is the right mouse button.
      Returns:
      true if the right mouse button was active
    • isMiddleMouseButton

      public final boolean isMiddleMouseButton()
      Returns true if the mouse event of this delegate is the middle mouse button.
      Returns:
      true if the middle mouse button was active
    • isAltDown

      public final boolean isAltDown()
      Returns whether the Alt modifier is down on the event of this delegate.
      Returns:
      true if the alt modifier is down
    • isCtrlDown

      public final boolean isCtrlDown()
      Returns whether the Control modifier is down on the event of this delegate.
      Returns:
      true if the control modifier is down
    • isShiftDown

      public final boolean isShiftDown()
      Returns whether the Shift modifier is down on the event of this delegate.
      Returns:
      true if the shift modifier is down
    • isMetaDown

      public final boolean isMetaDown()
      Returns whether the Meta modifier is down on the event of this delegate.
      Returns:
      true if the meta modifier is down
    • clickCount

      public final int clickCount()
      Returns the number of mouse clicks associated with the event of this delegate. You can use the returned number to distinguish between single-click and double-click events.
      Returns:
      An integer indicating the number of mouse clicks associated with the mouse event.
    • mouseX

      public final int mouseX()
      Returns the x coordinate of the mouse event of this delegate.
      Returns:
      integer value for the x coordinate
    • mouseY

      public final int mouseY()
      Returns the y coordinate of the mouse event of this delegate.
      Returns:
      integer value for the y coordinate
    • mousePosition

      public final Position mousePosition()
      Returns the position of the mouse event of this delegate in the form of an immutable Position value object. So you may safely share this object without having to worry about side effects.
      Returns:
      the position of the mouse event
    • mouseXOnScreen

      public final int mouseXOnScreen()
      Returns the absolute horizontal x position of the event of this delegate. In a virtual device multi-screen environment in which the desktop area could span multiple physical screen devices, this coordinate is relative to the virtual coordinate system. Otherwise, this coordinate is relative to the coordinate system associated with the Component's GraphicsConfiguration.
      Returns:
      x an integer indicating absolute horizontal position.
    • mouseYOnScreen

      public final int mouseYOnScreen()
      Returns the absolute vertical y position of the event of this delegate. In a virtual device multiscreen environment in which the desktop area could span multiple physical screen devices, this coordinate is relative to the virtual coordinate system. Otherwise, this coordinate is relative to the coordinate system associated with the Component's GraphicsConfiguration.
      Returns:
      y an integer indicating absolute vertical position.
    • mousePositionOnScreen

      public final Position mousePositionOnScreen()
      Returns the absolute x and y position of the event of this delegate in the form of an immutable Position value object. So you may safely share this object without having to worry about side effects.
      Returns:
      the absolute position of the mouse event