1

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.

fabian
  • 80,457
  • 12
  • 86
  • 114
grill2010
  • 574
  • 6
  • 23
  • 2
    Your calculation looks right, but `boundsInLocal` will not change when the user scrolls, so the listener won't be invoked and you won't recompute the scene bounds. Try adding a listener to the `localToSceneTransformProperty` – James_D Nov 08 '17 at 10:29
  • Omg, thank you. I spent almost the whole day yesterday figuring out how can I bind to the scroll event to recalculate the bounds. I thought I would have to subtract the offset by myself. I didn't expect that it is that simple. Sometimes you can't see the forest for the trees. – grill2010 Nov 08 '17 at 11:07
  • Stumbled across this old question, maybe you could add your comment as answer so that I can accept it? – grill2010 Jun 25 '18 at 08:57

1 Answers1

0

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.

grill2010
  • 574
  • 6
  • 23