Package swingtree

Class ComponentMouseEventDelegate<C extends JComponent>

Type Parameters:
C - The type of JComponent that this ComponentMouseEventDelegate is delegating to.
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 without DPI scaling. So the value is in "developer pixel" coordinate space relative to the underlying component. So a mouse position of (0,0) would be in the top left corner of the component.

      For Context:
      SwingTree supports high DPI scaling, but to achieve this it needs to scale the size of components for you depending on the current UI.scale() factor.
      This means that whatever dimensions you specify in methods like UIForAnySwing.withSizeExactly(Size) are not necessarily going to be the equal to the actual size of the underlying component.
      Therefore the component relative mouse position must be "unscaled" to be consistent from a developers point of view...

      Returns:
      An integer value representing the x coordinate without DPI scaling (aka "developer pixel").
    • mouseY

      public final int mouseY()
      Returns the y coordinate of the mouse event of this delegate without DPI scaling. So the value is in "developer pixel" coordinate space relative to the underlying component. So a mouse position of (0,0) would be in the top left corner of the component.

      For Context:
      SwingTree supports high DPI scaling, but to achieve this it needs to scale the size of components for you depending on the current UI.scale() factor.
      This means that whatever dimensions you specify in methods like UIForAnySwing.withSizeExactly(Size) are not necessarily going to be the equal to the actual size of the underlying component.
      Therefore the component relative mouse position must be "unscaled" to be consistent from a developers point of view...

      Returns:
      An integer value representing the y coordinate without DPI scaling (aka "developer pixel").
    • mousePosition

      public final Position mousePosition()
      Returns the Position of the mouse event of this delegate without DPI scaling. So the x and y coordinates of the position is scaled to the "developer pixel" coordinate space relative to the underlying component. So a position of (0,0) is in the top left corner of the component.

      For Context:
      SwingTree supports high DPI scaling, but to achieve this it needs to scale the size of components for you depending on the current UI.scale() factor.
      This means that whatever dimensions you specify in methods like UIForAnySwing.withSizeExactly(Size) are not necessarily going to be the equal to the actual size of the underlying component.
      Therefore the component relative mouse position must be "unscaled" to be consistent from a developers point of view...

      Returns:
      The component relative position of the mouse event in the developer pixel coordinate space (without DPI scaling applied).
    • 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