0

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?

Pavel_K
  • 10,748
  • 13
  • 73
  • 186

1 Answers1

0

Your code is constantly fighting against the layout mechanism of the StackPane. For absolut positioning of Nodes you should always use a Pane directly. In some situations it may also be possible to use an AnchorPane.

mipa
  • 10,369
  • 2
  • 16
  • 35