Drag and drop
A window that the user moves, a card that goes into a slot, an item dragged into an inventory: one setter makes a node follow the pointer. This page shows where the node may go, how it lands on a target, and how to drag a copy.
You will learn
- How to make a node draggable inside an area
- How to react when the drag starts, moves and ends
- How to drop a node on the nearest target
- How to drag a copy and keep the original in place
- How to allow the drag from a handle only
Make a node draggable
Section titled “Make a node draggable”Node.draggable takes a DraggableProperty:
RectNode.create(40, 40, 80, 80).color(DragUI.ACCENT).draggable(DraggableProperty.parent()).onDragDrop(event -> drops.increment()).attach(card);A press of the left button on the node starts the drag, and consumes the press.
While the button is down, the node eases toward the pointer; after the release it finishes its way and stops.
The node moves for good: its position, and a signal bound to x(...) or y(...), take the new value.
While it is dragged, the node is drawn above its siblings and receives the pointer first; it goes back to its place in the order at the drop.
Choose where the node may go
Section titled “Choose where the node may go”The factory of the property names the area that holds the node:
| Factory | The node stays inside |
|---|---|
DraggableProperty.parent() | Its parent node |
DraggableProperty.node(other) | Another node |
DraggableProperty.custom(x, y, width, height) | A rectangle of the design canvas |
DraggableProperty.ui() | The design canvas, 1920 by 1080 |
DraggableProperty.screen() | The part of the window that shows the UI |
DraggableProperty.free() | Nothing: it goes anywhere |
parent(), the pointer may leave the card, the square stays inside it.
A node dragged with parent() must have a parent: on a node attached to the UI itself, the drag throws an IllegalStateException.
The node moves through Node.move, so Node.beforeMove can refuse a position (Events).
React to the drag
Section titled “React to the drag”The square of the first example counts its drops:
.onDragDrop(event -> drops.increment())| Event | When it runs |
|---|---|
Node.beforeDragStart | The press asks for a drag; cancel() refuses it |
Node.onDragStart | The drag started |
Node.onDrag | The pointer moved during the drag |
Node.beforeDragDrop | The button was released; cancel() sends the node back where the drag started |
Node.onDragSnap | The node was dropped on a target (Drop on a target), before onDragDrop |
Node.onDragDrop | The node was dropped |
Each listener receives a NodeDragEvent; getTarget() is the target of the drop, or null.
Node.isDragging is true from the start to the release: in beforeDragDrop, the drag is already over and it reads false.
Node.startDragging and Node.stopDragging start and end a drag from your code, with the same events.
Drop on a target
Section titled “Drop on a target”snap(...) gives the property its targets.
At the drop, the node slides onto the nearest target, centered on it:
final RectNode left = RectNode.create(80, 180, 120, 120).color(SnapUI.ELEMENT).label(Text.create("A", info)).attach(card);final RectNode middle = RectNode.create(340, 180, 120, 120).color(SnapUI.ELEMENT).label(Text.create("B", info)).attach(card);final RectNode right = RectNode.create(600, 180, 120, 120).color(SnapUI.ELEMENT).label(Text.create("C", info)).attach(card);
RectNode.create(350, 40, 100, 100).color(SnapUI.ACCENT).draggable(DraggableProperty.parent().snap(left, middle, right)).onDragSnap(event -> slot.set(event.getTarget().getLabel().getText())).attach(card);The DraggableProperty.DraggableSnapType decides which target takes the node:
| Snap type | Target |
|---|---|
NEAREST | The target whose center is the nearest; there is always one |
OVERLAP | The first target that the node overlaps; with none, the node goes back where the drag started |
The node only slides to the target: your onDragSnap listener decides what the drop means, such as moving an item into a slot of your data.
Leave the original in place
Section titled “Leave the original in place”With DraggableProperty.DraggableType.COPY, the user drags a copy and the original stays where it is:
final RectNode slot = RectNode.create(480, 140, 240, 120).color(CopyUI.ELEMENT).attach(card);
RectNode.create(80, 160, 160, 80).color(CopyUI.ACCENT).label(Text.create("Sword", dark)).draggable(DraggableProperty.parent().type(DraggableType.COPY).snapType(DraggableSnapType.OVERLAP).snap(slot)).onDragSnap(event -> { event.getTarget().clearChildren(); RectNode.create(40, 20, 160, 80).color(CopyUI.ACCENT).label(Text.create("Sword", dark)).attach(event.getTarget());}).attach(card);onDragSnap puts a chip of its own in the slot.
JOID draws the copy above every node of the UI, under the pointer. Whatever happens at the drop, your tree changes only if a listener changes it. The events run on the original node.
Drag from a handle only
Section titled “Drag from a handle only”DraggableProperty.enabled takes a predicate that JOID tests when a drag would start.
This window moves only from its blue bar:
final RectNode handle = RectNode.create(0, 0, 300, 40).color(HandleUI.ACCENT).cursor(Cursor.MOVE);
RectNode.create(60, 60, 300, 200).color(HandleUI.ELEMENT).label(Text.create("Window", info)).draggable(DraggableProperty.parent().enabled(node -> handle.isHovered())).body(window -> handle.attach(window)).attach(card);enabled at the press.
A drag that started goes to its end, drop animation included, even when the pointer leaves the bar during a fast move.
DraggableProperty.disabled turns the drag off, and draggable(...) again replaces the property.
Know how a drag writes the position
Section titled “Know how a drag writes the position”A node dragged with DraggableType.MOVE must have a position that the drag can write: a x(...) or y(...) given as a lambda or as a signal that cannot be written throws an IllegalStateException.
Give it a value or a writable signal, or drag a copy.
draggable(property)makes a node follow the left button from a press on it to the release, inside the area of the property.- The node moves for good and is drawn above its siblings during the drag.
beforeDragStartandbeforeDragDropcan refuse the start or send the node back;onDragStart,onDrag,onDragDropandonDragSnaptell what happened.snap(targets...)slides the dropped node onto a target, the nearest one or the overlapped one (OVERLAP).DraggableType.COPYdrags a copy that disappears at the drop; the listeners decide what it leaves.enabled(predicate)decides whether a drag can start, for example from a handle.
Try it
Section titled “Try it”Count the drops in each slot.
In SnapUI, count how many times the square landed in each slot.
Solution
A MapSignal keeps one count per slot name:
.onDragSnap(event -> { final String name = event.getTarget().getLabel().getText(); counts.put(name, counts.peek().getOrDefault(name, 0) + 1);})Back home on a miss.
In SnapUI, send the square back to its start when the user drops it far from every slot.
Solution
OVERLAP keeps only the slots that the square covers, and sends it back when it covers none:
.draggable(DraggableProperty.parent().snapType(DraggableSnapType.OVERLAP).snap(left, middle, right))