JavaFX: How can I horizontally center an ImageView inside a ScrollPane? -
viewscroll.setcontent(new imageview(bigimg)); double w = viewscroll.getcontent().getboundsinlocal().getwidth(); double vw = viewscroll.getviewportbounds().getwidth(); viewscroll.getcontent().settranslatex((vw/2)-(w/2)); viewscroll.tofront();
i set imageview image inside scrollpane imageview goes far left corner. here i'm trying manually offset difference, doesn't work well. imageview goes far right plus updates once because it's inside eventhandler button.
here example using label without need listeners:
public void start(stage primarystage) throws exception { scrollpane scrollpane = new scrollpane(); label label = new label("hello!"); label.translatexproperty().bind(scrollpane.widthproperty().subtract(label.widthproperty()).divide(2)); scrollpane.setcontent(label); scene scene = new scene(scrollpane); primarystage.setscene(scene); primarystage.setwidth(200); primarystage.setheight(200); primarystage.show(); }
i not sure if familiar properties or not, in code above, when bind translatexproperty
formula, every time 1 of dependencies changes, such scrollpane
's widthproperty
or label
's widthproperty
, formula recalculated , translatexproperty
set result of formula.
i not sure, in previous code, appears calculation code in resize listener. not required when dealing properties update whenever dependencies changed (note bind()
, not set()
).
Comments
Post a Comment