Package swingtree

Class ComponentDragEventDelegate<C extends JComponent>

Type Parameters:
C - The type of JComponent that this ComponentDragEventDelegate is delegating to.

public final class ComponentDragEventDelegate<C extends JComponent> extends ComponentMouseEventDelegate<C>
A JComponent and MouseEvent delegate providing useful context information to Action listeners registered through UIForAnySwing.onMouseDrag(Action), like for example the ComponentMouseEventDelegate.mouseX() and ComponentMouseEventDelegate.mouseY() of the current event as well as more drag specific information like dragEvents(), dragPositions(), initialComponentPosition() and the deltaXSinceStart() / deltaYSinceStart() accumulated since the drag began.

A common use case is to move the dragged component along with the mouse pointer by combining the component's start position with the accumulated drag delta:


  UI.panel("fill")
  .onMouseDrag( e -> {
      e.setLocation(
          e.initialComponentPosition().x() + e.deltaXSinceStart(),
          e.initialComponentPosition().y() + e.deltaYSinceStart()
      );
      e.getParent().repaint();
  })
  

All position and delta values exposed by this delegate are reported in unscaled "developer pixels" (see UI.scale()), so they can be passed directly to other SwingTree APIs without further conversion. When interfacing with the underlying Swing API, scale them back up using UI.scale(int) or UI.scale(float).

  • Method Details

    • initialComponentPosition

      public Position initialComponentPosition()
      Returns the Position (relative to the parent container) that the dragged component had at the moment the user started the current continuous drag, i.e. when the mouse button was first pressed. The returned position remains constant for the entire duration of the drag, even if the component is moved in response to drag events.
      Combined with deltaXSinceStart() and deltaYSinceStart(), this is the typical building block for repositioning a component as it is being dragged:
      
        .onMouseDrag( e -> {
            Position start = e.initialComponentPosition();
            e.setLocation(
                start.x() + e.deltaXSinceStart(),
                start.y() + e.deltaYSinceStart()
            );
        })
        
      Note that the returned position is in "developer pixels" rather than the actual "UI scaled component space" of the underlying Swing component. Use UI.scale(int) to convert it back to the scaled component space if you need to interface with the raw Swing API.
      If for any reason no drag has been recorded yet (e.g. the history is empty), Position.origin() is returned as a sensible default.
      Returns:
      The unscaled Position of the component (relative to its parent) at the start of the current continuous drag, or Position.origin() if no drag has been recorded yet.
    • dragEvents

      public sprouts.Tuple<MouseEvent> dragEvents()
      Provides a Tuple (immutable list) of all MouseEvents of a continuous mouse drag performed on the component. When a drag ends, the tuple is empty.
      Returns:
      A tuple of all MouseEvents of a continuous mouse drag performed on the component.
    • dragPositions

      public sprouts.Tuple<Position> dragPositions()
      SwingTree keeps track of the most recent mouse drag events of a continuous drag. This method returns a Tuple (immutable list) of all mouse Positions of a continuous mouse drag performed on the component.
      Note that these points are scaled to "developer pixel" instead of the actual "UI scaled component space" of the underlying Swing component.
      Use UI.scale(int) to convert these points back to the actual "UI scaled component space". Also note that this method returns an unmodifiable list consisting of immutable Position objects instead of mutable Point objects, to protect the client from side effects.
      Returns:
      A tuple (immutable list) of all mouse Positions of a continuous mouse drag performed on the component. The points of this list represent the mouse movement track since the start of a continuous drag.
    • deltaXSinceStart

      public float deltaXSinceStart()
      Provides the x-axis movement delta of the mouse since the start of a continuous drag without DPI scaling. So the value is in "developer pixel" not in UI scaled component space. This means that when you need to interface with the underlying Swing API then you may want to consider upscaling it again using UI.scale(float).
      Returns:
      The x-axis movement delta of the mouse since the start of a continuous drag. This value is in "developer pixel" not in UI scaled component space.
    • deltaYSinceStart

      public float deltaYSinceStart()
      Provides the y-axis movement delta of the mouse since the start of a continuous drag without DPI scaling. So the value is in "developer pixel" not in UI scaled component space. This means that when you need to interface with the underlying Swing API then you may want to consider upscaling it again using UI.scale(float).
      Returns:
      The y-axis movement delta of the mouse since the start of a continuous drag. This value is in "developer pixel" not in UI scaled component space.