JavaFX labels height


I'm developing software in JavaFX and JavaFX Scene Builder.

I have a grid pane with 2 columns. Actually in each of the cells there are labels. In the first column of the table the text inside the label is a default constant, in the second one the text can change.

The problem is that if the text variable is too long, it's automatically truncated.

How to start a new line trimming the text and re-sizing the height of the rows in the grid pane?


Thanks to @fabian issues, this is the solution to the initial question. In addiction is necessary to set the fx:id of each element into Scene Builder.

@FXML private GridPane gridPane;
@FXML private Text text1;
@FXML private Text text2;
@FXML private Text text3;

private void setGridRowHeight(GridPane gpName, int numRow, double height){
    ObservableList<RowConstraints>  rows = gpName.getRowConstraints();

private void addTextListener(final GridPane gridPane, final Text text){

    text.textProperty().addListener(new ChangeListener<String>() {
        public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
            text.setWrappingWidth( gridPane.getColumnConstraints().get(1).getPrefWidth() );
            setGridRowHeight(gridPane, gridPane.getRowIndex(text), text.getLayoutBounds().getHeight() );

@FXML public void initialize() {
    addTextListener(gridPane, text1);
    addTextListener(gridPane, text2);
    addTextListener(gridPane, text3);
1/9/2014 2:55:39 AM

Accepted Answer

Unfortunately the Label element does not resize depending on the text displayed. I recommend using the Text Shape. You can specify the maximal width using wrapping width in the scene builder.

1/8/2014 1:36:20 PM

You could try:

Label col2Label = new Label();
col2Label.setWrapText( true );
col2Label.setMaxHeight( Double.MAX_VALUE );
col_1_Label.prefHeightProperty().bind( col2Label.heightProperty() );

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow