Javafx:如何摆脱可扩展的Textarea中的怪异包装效果?
你好,我的文字塔里亚面临问题。我的目标是制作一个可扩展的文本方面,这只是一个普通的文本方面,除了它没有滚动条,它包装文本,如果用户想调整文本方面的宽度大小,则需要更新高度。
除了一件事,一切都很好。假设我已经在文本方面输入了一段段落,然后减少了它的宽度,以便由于包裹而将1或2个字母推入下一行中,那么由于某些奇怪的原因,高度没有更新。
还是我的文本节点有问题?如果有人能帮助我,我会非常感谢,因为我已经坐了好几天了。
这是我的代码:
@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);
}
我的CSS样式表:
.text-area .scroll-pane {
-fx-hbar-policy: NEVER;
-fx-vbar-policy: NEVER;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能是您可以从以下解决方案开始,看看是否需要进一步的更改。
这个想法是要计算textarea的LayoutChildren()完成的高度,然后请求TextArea的布局以更新到计算高度。
您可能需要考虑其他条件,但我会留下来供您合作。
May be you can start with the below solution and see if you need any further changes.
The idea is to compute the height when the layoutChildren() of the TextArea is completed and then requesting the layout of TextArea to update to the computed height.
There may be other conditions you may need to consider, but I will leave that for you to work with.