JavaFX:TextArea自动高度扩展,无滚动条

时间:2016-12-18 21:47:13

标签: javafx-8 uitextarea

我可以强制 TextArea 控件自动扩展高度吗?

在以下情况中,我希望在 ScrollPane 控件处看到滚动条,而不是 TextArea 控件。

sp::rbind.SpatialPointsDataFrame

TextArea expanding the height

2 个答案:

答案 0 :(得分:1)

目前唯一能解决问题的解决方案是 @Uluk Biy ,我发现here,我所做的只是符合他的逻辑,并隐藏{ {1}}。 唯一的问题是与ScrollBars绑定的TextArea的大小,因此它在版本开头以Text开头,这是完整的代码:

minimum height

当然代码可以适应fxml格式,我只是没有足够的时间去做,这里是import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.geometry.Bounds; import javafx.scene.Scene; import javafx.scene.control.ScrollPane; import javafx.scene.control.ScrollPane.ScrollBarPolicy; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import javafx.scene.layout.Pane; import javafx.scene.text.Text; import javafx.stage.Stage; public class Launcher extends Application{ private Pane root = new Pane(); private Scene scene; private ScrollPane scroller; private Pane content = new Pane(); private TextField textF = new TextField(); private TextArea textA = new TextArea(); private Text textHolder = new Text(); private double oldHeight = 0; @Override public void start(Stage stage) throws Exception { root.getChildren().addAll(yourSP()); scene = new Scene(root,300,316); stage.setScene(scene); stage.show(); } private ScrollPane yourSP(){ content.setMinSize(300, 300); textF.setPrefSize(260, 40); textF.setLayoutX(20); textF.setLayoutY(20); textA.setPrefSize(260, 200); textA.setLayoutX(20); textA.setLayoutY(80); textA.getStylesheets().add(getClass().getResource("texta.css").toExternalForm()); content.getChildren().addAll(textA,textF); /*************************@Uluk Biy Code**************************/ textA.setWrapText(true); textHolder.textProperty().bind(textA.textProperty()); textHolder.layoutBoundsProperty().addListener(new ChangeListener<Bounds>() { @Override public void changed(ObservableValue<? extends Bounds> observable, Bounds oldValue, Bounds newValue) { if (oldHeight != newValue.getHeight()) { oldHeight = newValue.getHeight(); textA.setPrefHeight(textHolder.getLayoutBounds().getHeight() + 20); System.out.println(textHolder.getLayoutBounds().getHeight()); } } }); /****************************************************************/ scroller = new ScrollPane(content); scroller.setHbarPolicy(ScrollBarPolicy.NEVER); scroller.setPrefSize(300, 316); return scroller; } public static void main(String[] args) { launch(args); } } 的样式:

TextArea

继续祝你好运!

答案 1 :(得分:0)

我并不是说我喜欢这个解决方案。它像地狱一样糟糕,但实际上比带有 textHolder 的要好得多 - 更稳定。它是用 TornadoFX 用 Kotlin 编写的。对于 Java,它应该工作相同,但可能有更多的代码行 ;)

原理基本相同,但我们使用的是 textHolder 内的实际 Text 节点,而不是专用的 TextArea 对象。

class ExpandableTextArea : TextArea() {
    init {
        addClass("expandable")
        isWrapText = true

        children.onChange { a ->
            val scrollPane = a.list.first() as ScrollPane
            val contentView = scrollPane.content as Region
            contentView.childrenUnmodifiable.onChange { b ->
                b.next()
                if (b.list.size == 2) {
                    val group = b.list[1] as Group
                    group.children.onChange { c ->
                        val text = c.list.first() as Text
                        text.layoutBoundsProperty().onChange {
                            if (it != null) {
                                val targetHeight = it.height + font.size
                                prefHeight = targetHeight
                                minHeight = targetHeight
                            }
                        }
                    }
                }
            }
        }
    }
}

当然,您仍然需要相同的 CSS 来关闭滚动条:

.text-area > .scroll-pane{
  -fx-hbar-policy:never;
  -fx-vbar-policy:never; 
}

请注意,这里我还设置了 minHeight,因为我希望此节点“残酷地”扩展,因此它永远不必滚动。您可以删除它并保留设置 prefHeight 但如果没有足够的空间节点将停止增长并且可以通过鼠标滚动但没有滚动条,这可能会令人困惑。

注意:如果由于某些被遗忘的原因 TextArea 子树结构发生变化,这将会爆炸。

相关问题