Package swingtree
Class ComponentDragEventDelegate<C extends JComponent>
java.lang.Object
swingtree.AbstractDelegate<C>
swingtree.ComponentDelegate<C,MouseEvent>
swingtree.ComponentMouseEventDelegate<C>
swingtree.ComponentDragEventDelegate<C>
- Type Parameters:
C- The type ofJComponentthat thisComponentDragEventDelegateis 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 Summary
Modifier and TypeMethodDescriptionfloatProvides the x-axis movement delta of the mouse since the start of a continuous drag without DPI scaling.floatProvides the y-axis movement delta of the mouse since the start of a continuous drag without DPI scaling.sprouts.Tuple<MouseEvent> Provides aTuple(immutable list) of allMouseEvents of a continuous mouse drag performed on the component.sprouts.Tuple<Position> SwingTree keeps track of the most recent mouse drag events of a continuous drag.Returns thePosition(relative to the parent container) that the dragged component had at the moment the user started the current continuous drag, i.e.Methods inherited from class swingtree.ComponentMouseEventDelegate
clickCount, isAltDown, isCtrlDown, isLeftMouseButton, isMetaDown, isMiddleMouseButton, isRightMouseButton, isShiftDown, mousePosition, mousePositionOnScreen, mouseX, mouseXOnScreen, mouseY, mouseYOnScreenMethods inherited from class swingtree.ComponentDelegate
forComponent, forSiblinghood, forSiblinghoodOfType, forSiblings, forSiblingsOfType, getComponent, getEvent, getSiblinghood, getSiblinghoodOfType, getSiblings, getSiblingsOfTypeMethods inherited from class swingtree.AbstractDelegate
_component, _isUndefinedColor, _isUndefinedFont, _siblingsSource, animateFor, animateFor, animateFor, animateFor, animateStyleFor, animateStyleFor, find, find, find, findAll, findAllByGroup, findAllByGroup, findAllByGroup, findAllByGroup, get, getBackground, getBorder, getBounds, getCursor, getFont, getForeground, getHeight, getLocation, getMaxSize, getMinSize, getParent, getPrefSize, getSize, getTooltip, getWidth, getX, getY, isEnabled, isOpaque, isVisible, paint, paint, paint, paint, parentDelegate, setBackground, setBackgroundColor, setBackgroundColor, setBackgroundColor, setBackgroundColor, setBorder, setBounds, setBounds, setBounds, setCursor, setCursor, setEnabled, setFont, setForeground, setForegroundColor, setForegroundColor, setForegroundColor, setForegroundColor, setHeight, setLocation, setMaxHeight, setMaxSize, setMaxSize, setMaxSize, setMaxWidth, setMinHeight, setMinSize, setMinSize, setMinSize, setMinWidth, setPrefHeight, setPrefSize, setPrefSize, setPrefSize, setPrefWidth, setSize, setSize, setSize, setTooltip, setVisible, setWidth, shapeOf, style
-
Method Details
-
initialComponentPosition
Returns thePosition(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 withdeltaXSinceStart()anddeltaYSinceStart(), this is the typical building block for repositioning a component as it is being dragged:
Note that the returned position is in "developer pixels" rather than the actual "UI scaled component space" of the underlying Swing component. Use.onMouseDrag( e -> { Position start = e.initialComponentPosition(); e.setLocation( start.x() + e.deltaXSinceStart(), start.y() + e.deltaYSinceStart() ); })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
Positionof the component (relative to its parent) at the start of the current continuous drag, orPosition.origin()if no drag has been recorded yet.
-
dragEvents
Provides aTuple(immutable list) of allMouseEvents 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
SwingTree keeps track of the most recent mouse drag events of a continuous drag. This method returns aTuple(immutable list) of all mousePositions 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.
UseUI.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 immutablePositionobjects instead of mutablePointobjects, 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 usingUI.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 usingUI.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.
-