Results 1 to 3 of 3
  1. #1
    diolu is offline Member
    Join Date
    Jan 2017
    Posts
    26
    Rep Power
    0

    Default Center the node in a scrollpane.

    I would like to put a node in a ScrollPane. If the view size is too small to view the node completely, then scroll as normal. But if the view size is large enough, the node should be centered. This is common to view image. Display the image centered but if the window is resized and the view size become too small, then the scrollbar appear. The scrollpane just display the node top left in this case.

    Has anyone an idea? Is this possible to do this in a simple way? (not by overriding poorly documented internal method).

    Updated: This work by putting a stackpane inside the scrollpane and the node in the stackpane; using the setFitToHeight and setFitToWidth of the stackpane. I do not know if this is a clean solution.
    Last edited by diolu; 03-03-2017 at 05:49 PM.

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Center the node in a scrollpane.

    Quick wrap-up for one dimension (height):
    you can determine the dimensions of the content in the scrollPane with something like
    Java FX Code:
    Double contentHeight = scrollPane.getContent().getBoundsInLocal().getHeight();
    In a scrollPane you can set the viewport. The height of the viewport is
    Java FX Code:
    Double viewportHeight = scrollPane.getViewportBounds().getHeight();
    The center of the content as displayed can be calculated by averaging getMax() and getMin() of node.getBoundsInParent().
    The methods for setting the viewport are called scrollPane.setVvalue() and scrollPane.setHvalue().

    All other questions are answered in the Math department - LOL

  3. #3
    diolu is offline Member
    Join Date
    Jan 2017
    Posts
    26
    Rep Power
    0

    Default Re: Center the node in a scrollpane.

    @benji2505 Thank you very much. Indeed I can do all the computation I need with these simple methods. Being new to java and GUI programming, I had overlooked getViewportBounds() methods and the getBoundsInParent() .

Similar Threads

  1. Need help with some ScrollPane() features
    By paulhr in forum JavaFX
    Replies: 0
    Last Post: 08-20-2015, 07:39 PM
  2. Replies: 1
    Last Post: 08-12-2012, 03:15 AM
  3. how to add a scrollpane to Jframe
    By yaminisankar in forum AWT / Swing
    Replies: 1
    Last Post: 07-09-2009, 09:56 AM
  4. ScrollPane Issues..
    By hanifa in forum AWT / Swing
    Replies: 4
    Last Post: 09-11-2008, 08:18 AM
  5. scrollpane
    By Newb in forum AWT / Swing
    Replies: 0
    Last Post: 06-09-2008, 12:14 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •