5

What I want to do:
I've got a JavaFX ScrollPane and I need to determine the area visible in the ScrollPane. I know about ScrollPane.getViewPortBounds(), which allows me to get the size of the visible area, but not the position.

Is there any way I can do this?


In context:
I'm displaying a very large image, which needs to be displayed in only portions at a time. The entire ScrollPane is used to scroll the image, but to determine which parts of the image I need to load, I need to know the visible area displayed in the ScrollPane.

midrare
  • 2,371
  • 28
  • 48

1 Answers1

1

ScrollPane also provides the visible area. This code seems to work:

    Bounds bounds = scrollPane.getViewportBounds();
    int lowestXPixelShown = -1 * (int)bounds.getMinX() + 1;
    int highestXPixelShown = -1 * (int)bounds.getMinX() + (int)bounds.getMaxX();
John Cashew
  • 1,078
  • 2
  • 12
  • 28
  • 1
    Hmm.. I've played with this a little, and it does seem to work, but only reflects changes made by resizing the window. Scrolling the scroll bars doesn't update `viewportBounds`, which now appears to by the cause of my confusion. I'll make a separate question for the non-updating `viewportBounds`. – midrare Oct 07 '14 at 03:46
  • Are you scrolling your scrollbar with setHvalue() or manually? Maybe it doesn't matter because getViewportBounds() updates for me either way. Does your app call getViewportBounds() again after scrolling? It needs to. Sorry if that is a dumb suggestion! – John Cashew Oct 07 '14 at 18:31
  • I'm scrolling by dragging the scroll bar with my mouse. I took your suggestion and experimented a little. In my test app [(click here)](http://pastie.org/private/zsjaojwtytphephoxp3rg), only calling `getViewportBounds()` by clicking a `Button` works, but for some reason, not from a `ChangeListener`, which I was previously using. The `Button` route isn't a viable option for me, though - I need automation so I'll have to get the `ChangeListener` working. – midrare Oct 07 '14 at 22:19
  • I think the events you that can trigger changes to the viewport bounds are scroll and resize events. These two pages show how to handle those events: http://docs.oracle.com/javafx/2/ui_controls/scrollbar.htm http://dlemmermann.wordpress.com/2014/04/10/javafx-tip-1-resizable-canvas/ – John Cashew Oct 08 '14 at 00:56
  • @JohnCashew I thought the point of a property was that it allowed you to treat it as an always up to date value, sure I could call `.getViewportBounds();` all the time, but instead I thought I could just listen to the `Bounds` property... Is my fundamental understanding of a property incorrect? – Troyseph Aug 21 '16 at 09:46
  • 3
    This code doesn't give visible bounds of a scroll pane. Only view port bounds. – Shah Rukh Qasim Sep 27 '17 at 07:08
  • `highestXPixelShown` doesn't change with this code during scrolling (at least in JavaFX 11). I think it should be `int highestXPixelShown = -1 * (int)bounds.getMinX() + (int)bounds.getWidth();` – helospark Feb 17 '19 at 11:41
  • This does not work in JFX 16 - I would expect as you scroll right, lowestXPixelShown would increase (and decrease when scrolling left). This does not happen. – NullPumpkinException Dec 05 '21 at 00:01