Sunday 15 April 2012

java - How to drag a JavaFX node and detect a drop event outside the JavaFX Windows? -



java - How to drag a JavaFX node and detect a drop event outside the JavaFX Windows? -

i'm implementing tabpane can detached javafx window. when tab pane dragged outside of window came from, new window created , tab placed in window.

i implemented methods using drag gestures drag tab between existing windows. however, not able receive mouse events or drag events when mouse moved outside of javafx scenes. possible?

you can hear mouse released event on appropriate node (e.g. tab's graphic). check screen coordinates using mouseevent.getscreenx() , mouseevent.getscreeny() , see if lie outside current window. if do, create new window, scene, , tab pane; remove tab current tab pane , place in new one.

here's basic illustration no frills (e.g. no user hints fact dragging occuring), give idea:

import javafx.application.application; import javafx.collections.listchangelistener.change; import javafx.geometry.point2d; import javafx.geometry.rectangle2d; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.control.label; import javafx.scene.control.tab; import javafx.scene.control.tabpane; import javafx.scene.control.textarea; import javafx.scene.layout.borderpane; import javafx.stage.stage; import javafx.stage.window; public class detachabletabexample extends application { private int tabcount = 0 ; @override public void start(stage primarystage) { tabpane tabpane = new tabpane(); button newtabbutton = new button("new tab"); newtabbutton.setonaction(event -> { tab tab = new tab(); label tablabel = new label("tab "+(++tabcount)); tab.setgraphic(tablabel); tab.setcontent(new textarea()); tabpane.gettabs().add(tab); tablabel.setonmousereleased(me -> { point2d mouseloc = new point2d(me.getscreenx(), me.getscreeny()); window window = tabpane.getscene().getwindow(); rectangle2d windowbounds = new rectangle2d(window.getx(), window.gety(), window.getwidth(), window.getheight()); if (! windowbounds.contains(mouseloc)) { tabpane.gettabs().remove(tab); stage newstage = new stage(); tabpane newtabpane = new tabpane(); newtabpane.gettabs().add(tab); scene scene = new scene(new borderpane(newtabpane)); newstage.setscene(scene); newstage.setx(me.getscreenx()); newstage.sety(me.getscreeny()); newstage.setwidth(window.getwidth()); newstage.setheight(window.getheight()); newstage.show(); } }); }); borderpane root = new borderpane(tabpane, newtabbutton, null, null, null); scene scene = new scene(root, 600, 400); primarystage.setscene(scene); primarystage.show(); } public static void main(string[] args) { launch(args); } }

java javafx javafx-8

No comments:

Post a Comment