I wrote a basic control and its skin. A label
is displayed in a HBox
in the skin. This label should wrap its 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 it is too long");
label.setWrapText(true);
box.getChildren().add(label);
getChildren().add(box);
}
}
}
But the label does not wrap (the ellipsis is displayed) because the preferred width of my control is not correctly computed:
what i want to obtain is:
How can i configure the skin to compute the skin preferred height to obtain the desired behavior (i never want an ellipsis displayed) ?
Notes:
- i don't want to set an explicit maximum size on the label or on other skin components:
label.setMaxWidth(150)
. The sole explicit width set should be the rootBorderPane
in thestart method
. This width (150) could be variable, the control could be used in different place. - this basic control is of course a simplification of the real one. The real one displays several
Label
with variable texts inside. - the label wraps correctly if i augment the window height until it has enough space
- this code is running on java 1.8.0_40-b27 on a OSX 10.10.2