Getting the global coordinate of a Node in JavaFX


How can I get the actual position of a node in the scene. The absolute position, regardless of any containers/transforms.

For example, I want to translate a certain node a so that it would temporarily overlap another node b. So I wish to set his translateX property to b.globalX-a.globalX.

The documentation says:

Defines the X coordinate of the translation that is added to the transformed coordinates of this Node for the purpose of layout. Containers or Groups performing layout will set this variable relative to layoutBounds.minX in order to position the node at the desired layout location.

For example, if child should have a final location of finalX:

 child.layoutX = finalX - child.layoutBounds.minX;

That is, the final coordinates of any node should be

finalX = node.layoutX + node.layoutBounds.minX

However running the following code:

var rect;
Stage {
    title: "Application title"
    width: 250
    scene: Scene {
        content: [
            Stack{content:[rect = Rectangle { width:10 height:10}] layoutX:10}

println("finalX = {rect.layoutX+rect.layoutBounds.minX}");

gives me finalX = 0.0 instead of finalX = 10.0 as the docs seemingly state.

Is there a clear method to get the absolutely final positioning coordinates in JavaFX?

1/17/2018 2:06:08 AM

Accepted Answer

The only solution I found so far is

rect.localToScene(rect.layoutBounds.minX, rect.layoutBounds.minY) // a Point2D{x:Float y:Float} object

Which doesn't seem to me as the "best" way to do that (note that this function is not bound). Still it works for JavaFX 1.2.

10/7/2009 2:27:06 PM

For bounds:

bounds = rect.localToScene(rect.getBoundsInLocal());

Work for JavaFx 1 and 2.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow