java - How to execute code during a drag and drop operation? -
i have node want implement drag , drop (this object source not target). want object move along mouse cursor. managed both of these not @ same time.
it appears setondragdetected
, setonmousedragged
don't work together. consider node following handlers:
import javafx.application.application; import javafx.scene.group; import javafx.scene.scene; import javafx.scene.input.clipboardcontent; import javafx.scene.input.dragboard; import javafx.scene.input.transfermode; import javafx.scene.shape.rectangle; import javafx.stage.stage; public class example extends application { @override public void start(stage primarystage) throws exception { rectangle rect = new rectangle(20, 20); rect.setonmousepressed(e -> system.out.println("pressed")); rect.setonmousedragged(e -> system.out.println("dragged")); rect.setondragdetected(e -> { system.out.println("detected"); clipboardcontent content = new clipboardcontent(); content.putstring("something"); dragboard db = rect.startdraganddrop(transfermode.any); db.setcontent(content); }); group subgroup = new group(rect); scene scene = new scene(subgroup, 100, 100); primarystage.setscene(scene); primarystage.show(); } public static void main(string[] args) { example.launch(args); } }
now press mouse on node , move mouse. output:
pressed dragged dragged dragged dragged dragged dragged detected
once drag detected mousedragged
handler stops.
how achieve described? 1 thing noticed maybe can use ondragover
parent want behavior in node because that's should be.
you mixing 2 things here. in short, when call startdraganddrop
method system switches drag , drop mode , java stops delivering mouseevent
rect
.
the mouseevent documentation has section "dragging gestures" explains 3 types of dragging gestures. here short summary:
- simple press-drag-release - when drag detected java continues deliver
mouseevents
node drag detected. - full press-drag-release - can call
startfulldrag
inside handler setsetondragdetected
. java starts delivermousedragevents
other nodes (potential gesture targets). - platform-supported drag-and-drop - if call
startdraganddrop
insideondragdetected
handler, java stop delivermouseevents
, start deliverdragevents
instead. used drag , drop interaction other applications.
it not clear me want achieve, long not want drag outside of application, try using startfulldrag
instead.
also, might helpful have further @ dragevent , mousedragevent documentation.
Comments
Post a Comment