java - How can I get the width and the height of a JavaFX Label? -
there 2 methods getwidth
, getheight
return old label size if changed label
text value.
for example, in code, background not correctly resized:
label speedlabel = new label(); rectangle backgroundlabel = new rectangle(); // initialization // change label text speedlabel.settext(connection.getspeed_bps()); // sets bigger number // adjust background backgroundlabel.setwidth(speedlabel.getwidth()); backgroundlabel.setheight(speedlabel.getheight());
after initialization, label
that:
and after text change , background adjustment, label
that:
i had @ post recommends deprecated method:
the reason returning "old" size label doesn't updated on gui in moment when set textproperty
of it, therefore size not changed.
you can listen widthproperty , heightproperty of label
, resize rectangle
in listener:
speedlabel.widthproperty().addlistener((obs, oldval, newval) -> { backgroundlabel.setwidth(newval.doublevalue()); }); speedlabel.heightproperty().addlistener((obs, oldval, newval) -> { backgroundlabel.setheight(newval.doublevalue()); });
or use bindings between properties:
backgroundlabel.heightproperty().bind(speedlabel.heightproperty()); backgroundlabel.widthproperty().bind(speedlabel.widthproperty());
but if want achieve label
background, don't need rectangle
, css - can check question: fxml stackpane doesn't align children correctly
Comments
Post a Comment