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?
EDIT:
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();
rows.get(numRow).setMinHeight(32);
rows.get(numRow).setPrefHeight(height);
rows.get(numRow).setMaxHeight(height);
}
private void addTextListener(final GridPane gridPane, final Text text){
text.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
scrollPane.setFitToHeight(true);
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);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With