自动向下滚动TextArea
我有一个TextArea
, 当我在其中添加文本时 ,它不会向下滚动 。 我想使用这个答案 ,但我的TextArea
连接到StringProperty
如下所示:
consoleTextArea.textProperty().bind(textRecu);
所以答案对我不起作用, 是否有另一种方法可以让我的TextArea
每次通过绑定更新时都向下滚动?
以下是关于将监听器添加到textRecu的评论意思的快速演示。 是的,因为绑定,无法更改consoleTextArea.textProperty()。 但textRecu没有绑定=>可以更改,我们可以添加监听器。
import javafx.application.Application; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextArea; import javafx.scene.input.MouseEvent; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class Main extends Application { private StringProperty textRecu = new SimpleStringProperty(); private TextArea consoleTextArea = new TextArea(); @Override public void start(Stage primaryStage) throws Exception{ VBox root = new VBox(); Button button = new Button("Add some text"); button.setOnMouseClicked(new EventHandler() { @Override public void handle(MouseEvent event) { //here you change textRecu and not consoleTextArea.textProperty() textRecu.setValue(textRecu.getValue() +"New Line\n"); } }); root.getChildren().addAll(consoleTextArea, button); consoleTextArea.textProperty().bind(textRecu); //here you also add listener to the textRecu textRecu.addListener(new ChangeListener