Hi and thanks in advance for any advice.
Basically I have a flowpane as the content of a scrollpane for showing a user Buttons containing data. I need to be able to drag these anywhere on the screen but the buttons clip at the edges of the scrollpane. I use this code for dragging the buttons:
dataButton.setOnMousePressed(e -> {
nodeCoordinates[0] = e.getSceneX() - dataButton.getTranslateX();
nodeCoordinates[1] = e.getSceneY() - dataButton.getTranslateY();
dragData = data;
});
dataButton.setOnMouseDragged(e -> {
dataButton.setTranslateX(e.getSceneX()-nodeCoordinates[0]);
dataButton.setTranslateY(e.getSceneY()-nodeCoordinates[1]);
});
dataButton.setOnMouseReleased(e -> {
dataButton.setTranslateX(0);
dataButton.setTranslateY(0);
if(e.getButton().equals(MouseButton.SECONDARY)){
openDataPanel((Credential) data);
}
DragClearer clear= new DragClearer();//An extension of Thread for nulling a drag variable I have which gets filled with the respective data when the button is dragged
clear.start();
});
the flowpane is what contains each button and the scrollpane doesnt take up the entire screen there are other things displayed. I need the buttons to return to their original position unless they are dragged to specific areas which they do. A while a go i did use the in-built dragging and dropping but didnt really work for what I needed it to. I would say im an intermediate at javaFX so my knowledge doesnt really go that far. And when searching online I havent really found anything similar to my problem so im either doing something wrong or im definetly doing something wrong.
Again any advice you guys can give is really appreciated.
Here is an example that hopefully can get you in the right direction.
This code takes a snapshot of the node and then sets the node invisible. The snapshot is draggable to any part of the
Scene. On release on a target node, the Button is recreated using information from the original button. I haven't tried it, but I would guess that on release on a target node, you can add the button as a child to the target node.I altered the code from here. @James_D answer may be better from here. I did not try his code.
Main
Output