java - Preferred height of a control skin containing a wrapped label -
i wrote basic control , skin. label displayed in hbox in skin. label should wrap text if there isn't enough space.
public class labelwrap extends application { public static void main(string[] args) { launch(labelwrap.class); } @override public void start(stage stage) throws exception { basiccontrol basiccontrol = new basiccontrol(); borderpane borderpane = new borderpane(); borderpane.setprefwidth(150); borderpane.setcenter(basiccontrol); stage.setscene(new scene(borderpane)); stage.centeronscreen(); stage.show(); } private static class basiccontrol extends control { @override protected skin<?> createdefaultskin() { return new basiccontrolskin(this); } } private static class basiccontrolskin extends skinbase<basiccontrol> { protected basiccontrolskin(basiccontrol control) { super(control); vbox box = new vbox(); label label = new label("this text should wrap because long"); label.setwraptext(true); box.getchildren().add(label); getchildren().add(box); } } } but label not wrap (the ellipsis displayed) because preferred width of control not correctly computed:

what want obtain is:

how can configure the skin compute skin preferred height obtain desired behavior (i never want ellipsis displayed) ?
notes:
- i don't want set explicit maximum size on label or on other skin components:
label.setmaxwidth(150). sole explicit width set should rootborderpaneinstart method. width (150) variable, control used in different place. - this basic control of course simplification of real one. real 1 displays several
labelvariable texts inside. - the label wraps correctly if augment window height until has enough space
- this code running on java 1.8.0_40-b27 on osx 10.10.2
afaik, wrap text in label should define width label because referring setwraptext(boolean) documentation:
public final void setwraptext(boolean value)
sets value of property wraptext.
property description: if run of text exceeds width of labeled, variable indicates whether text should wrap onto line.
here statement exceeds width of labeled induce have defined width label, that's why can't use when there's no width defined.
so code should be:
label label = new label("this text should wrap because long"); label.setmaxwidth(150); label.setwraptext(true); another alternative use text element instead of label , use method setwrappingwidth() this:
text t = new text("this text should wrap because long" ); t.setwrappingwidth(150); and result:

conclusion:
to wrap text (either in label or in text element) have define width text return new line when exceed width.
edit:
and make little bit more dynamic , avoid setting width label, , if setting prefwidth borderpaneyou can use static double width prefwidth , set maxwidth of label, here's example code:
public class labelwrap extends application { static double width; public static void main(string[] args) { launch(labelwrap.class); } @override public void start(stage stage) throws exception { basiccontrol basiccontrol = new basiccontrol(); borderpane borderpane = new borderpane(); borderpane.setprefwidth(150); borderpane.setcenter(basiccontrol); //get prefwidth value in width attribute width = borderpane.getprefwidth(); stage.setscene(new scene(borderpane)); stage.centeronscreen(); stage.show(); } private static class basiccontrol extends control { @override protected skin<?> createdefaultskin() { return new basiccontrolskin(this); } } private static class basiccontrolskin extends skinbase<basiccontrol> { protected basiccontrolskin(basiccontrol control) { super(control); vbox box = new vbox(); label label = new label("this text should wrap because long"); //set width value label maxwidth label.setmaxwidth(width); label.setwraptext(true); box.getchildren().add(label); this.getchildren().add(box); } } }
Comments
Post a Comment