hello iam facing a problem with my textarea. my goal is to make an expandable textarea which is just a normal textarea except it has no scrollbar, it wraps text and if the user wants to resize the width of the textarea the height needs to be updated.
everything works fine except one thing. lets say I already typed a paragraph into my textarea and then reduce the width of it just as much so that 1 or 2 letter are pushed in the next line because of wrapping then the height is not updating for some weird reason.
iam not sure what iam doing wrong, do i use wrong listeners? or is there an problem with my text node? i would be very thankful if anyone can help me because iam sitting at this problem for days now.
here is my code:
@Override
public void start(Stage stage) throws IOException {
VBox box = new VBox();
TextArea area = new TextArea();
area.setWrapText(true);
area.setMinHeight(27);
area.setPrefHeight(27);
area.setMaxHeight(27);
box.getChildren().add(area);
Scene scene = new Scene(box);
scene.getStylesheets().add(this.getClass().getResource("/gui/dumps/test.css").toExternalForm());
stage.setScene(scene);
stage.show();
area.textProperty().addListener((obs, old, niu) -> {
setHeight(area);
});
area.layoutBoundsProperty().addListener((observable, oldValue, newValue) -> {
setHeight(area);
});
}
public void setHeight(TextArea area) {
Text text = (Text) area.lookup(".text");
double height = text.getLayoutBounds().getHeight() + 10;
area.setMinHeight(height);
area.setPrefHeight(height);
area.setMaxHeight(height);
}
public static void main(String[] args) {
launch(args);
}
and my css stylesheet:
.text-area .scroll-pane {
-fx-hbar-policy: NEVER;
-fx-vbar-policy: NEVER;
}