java - JavaFX ScrollPane programmatically moving the viewport - centering content -
i can use setvvalue(double) , sethvalue(double) methods move viewport in javafx scrollpane. i'm struggling center particular node in content of scroll pane based on position. i've tried sorts of combos of localtoscene() , boundsinparent(). i've read (a lot) , seen example
how scroll make node within content of scrollpane visible?
which close doesn't center objects puts them visible. having built in mouse panning brilliant i'm making heavy weather of programmatic panning.
ultimately need able zoom have put actual shapes in group , added group scroll pane content. think i'm supposed zooming on group , again need able zoom around center of group manipulating , identifying current center position. pointers or examples can provided really appreciated. code sample in link above sscce.
thanks in advance,
andy
i'll try explaining without code, because don't think that's necessary here.
let's content of scrollpane has height h
, , height of viewport v
. if h = v
, content fit viewport, , need no scrollbar. in situation (with non-movable scrollbar), element centered, need positioned @ center of scrollpane's content. can't move viewport's center means of scrolling.
now consider h
twice size of v
(i.e. h = 2v
). in situation upper 1/4 , lower 1/4 of scrollpane's content not centered scrolling.
(if absolutely needed center component scrolling should consider padding content pane, we'll continue unpadded solution here)
when think it, you'll realize possible srollable distance of scrollbar h - v
, , scroll amount setting vvalue
1.0.
to center point y
(here point y coordinate of scrollpane's content pane) can use following vvalue:
vvalue = (y - 0.5 * v) / (h - v)
the nominator of expression y-coordinate of shown @ top of viewport when point y centered inside viewport. denominator total scrollable distance.
edit: adding code anyway!
public void centernodeinscrollpane(scrollpane scrollpane, node node) { double h = scrollpane.getcontent().getboundsinlocal().getheight(); double y = (node.getboundsinparent().getmaxy() + node.getboundsinparent().getminy()) / 2.0; double v = scrollpane.getviewportbounds().getheight(); scrollpane.setvvalue(scrollpane.getvmax() * ((y - 0.5 * v) / (h - v))); }
(please note assumes node direct child of scrollpane's contentpane)
hope helps! :)
Comments
Post a Comment