Search code examples
javajavafxscrollpane

JavaFX - get scene coordinates of child in ScrollPane


I'm trying to calculate the scene coordinates from a child inside a ScrollPane. We did this on several other nodes in our project with this method

this.boundsInLocalProperty().addListener( ( observable, oldValue, newValue ) ->
{
     final Bounds boundsOnScene = this.localToScene( newValue );
     ..
} );

It is really straightforward but how can I calculate the coordinates of the children in a ScrollPane even when the specific node is currently out of the view bounds? Our FXML structure looks like this

<ScrollPane>
   <VBox>
      <Pane/>
      <Pane/>
      <Pane/>
   </VBox>
</ScrollPane>

I've searched a while now on the internet but I can not figure out where I should start. Maybe someone could guide me in the right direction?


Edit: thanks to comment of James_D I figured out what was missing. In a ScrollPane the boundsInLocalProperty listener will not be invoked during scrolling, so I had to add

this.localToSceneTransformProperty().addListener( ( observable, oldValue, newValue ) ->
{
     final Bounds boundsOnScene = this.localToScene( this.getBoundsInLocal() );
     ...
} );

to take the scrolling behavior into account.


Solution

  • Thanks to James_D I figured out what was going wrong.

    The boundsInLocal will not change when the user scrolls. I also needed to listen to the localToSceneTransformProperty to recompute the scene bounds.