I have a pane (1440x800) and a node (400x300) which I need to move inside this pane. So for Pane I took StackPane and for Node I took VBox.
This is the controller which moves the node.
public class Controller extends AbstractController {
/**
* Mouse pressed X position.
*/
private double mousePressedX;
/**
* Mouse pressed Y position.
*/
private double mousePressedY;
/**
* Insets that were when user pressed mouse.
*/
private Insets mousePressedInsets;
/**
* Mouse pressed handler.
* @param event
*/
@FXML
public void handleTitlePaneMousePressed(final MouseEvent event) {
Node node = (Node) getView().getFxView();
mousePressedX = event.getScreenX();
mousePressedY = event.getScreenY();
mousePressedInsets = StackPane.getMargin(node);
if (mousePressedInsets == null) {
mousePressedInsets = new Insets(0, 0, 0, 0);
}
}
/**
* Moused dragged handler.
* @param event
*/
@FXML
public void handleTitlePaneMouseDragged(final MouseEvent event) {
Node node = (Node) getView().getFxView();
double deltaX = event.getScreenX() - mousePressedX;
double deltaY = event.getScreenY() - mousePressedY;
Insets newInsets =
new Insets(mousePressedInsets.getTop() + deltaY, 0, 0, mousePressedInsets.getLeft() + deltaX);
StackPane.setMargin(node, newInsets);
}
}
When user clicks on title of the node then pressed X and Y are saved and used in Moused dragged handler
. The code works as I need. The only problem is that there is a little lag between mouse move and node mode.
I tried to add the following settings it JVM
-Djavafx.animation.fullspeed=true
-Dprism.vsync=false
But it didn't help, maybe because I use Linux - don't know.
Is there another more optimal way move absolute positioned Node in Pane in JavaFX?