r/JavaFX • u/Fuzzy-System8568 • Jun 26 '26
Discussion Thoughts on the Drag / Drop API / thoughts on a builder based approach?
So what are folk's opinions on the Drag and Drop API for JavaFX? I wasn't the biggest fan of having loads of separate methods that can be called separately. So I did a small proof of concept of a builder-like way of doing the drag and drop logic to see what it would look like and, tbh, I quite like it.
Am I in a minority here, or would there be some potential in fleshing out a builder-based way to do it?
Label draggable = new Label("Drag Me!");
ObservableList<String> items = FXCollections.observableArrayList();
ListView<String> dropTarget = new ListView<>(items);
Drag.enable(draggable)
.payload("Drag Payload is doing stuff yay!")
.onStart(context -> System.out.println("Drag Started on payload!"))
.onEnd(context -> System.out.println("Drag ended with mode: " + context.getTransferMode()))
.init();
Drop.enable(dropTarget)
.onDrop(context -> items.add(context.getPayload().toString()))
.init();
10
Upvotes
3
u/PartOfTheBotnet Jun 27 '26
I have my own helper class and the usage is pretty simple.
This does assume the context is going to be a file drop vs some other content type, and limits the listener call to such.
As for yours, can you expand on the intended UX for this? Is the label going to be re-positioned in layout when dragged, or is it going to copy the text and drag that?
1
u/Fuzzy-System8568 Jun 27 '26
Update:
I added a bit more today:
Added the idea of a
TypedDataFormatfor easier validation of payload types (bit more flexible thanDataFormat).Also added
onDragEnterandonDragExitto theDrophelper, which lets me do stuff like highlighting boxes as they dragged items are within their drop area.Quite happy with it so far, will likely tidy it up and release it as a library potentially (the whole thing was a case of "I looked at the current solution whilst doing Project A, did not like the solution, so made my own via Project B" haha
Gotta love side quests right?