【问题标题】:Auto scroll down a TextArea自动向下滚动 TextArea
【发布时间】:2015-06-01 12:25:51
【问题描述】:

我有一个TextArea在我添加文本时不会向下滚动。我想使用this answer,但我的TextArea 像这样连接到StringProperty

consoleTextArea.textProperty().bind(textRecu);

所以答案对我不起作用,还有其他方法可以让我的TextArea每次更新时通过绑定向下滚动? p>

【问题讨论】:

  • 您可能可以将侦听器添加到 textRecu StringProperty。
  • 嗯,不,我在帖子中说过,由于绑定我认为发生变化时不会调用侦听器...

标签: java scroll javafx textarea


【解决方案1】:

这是我在评论中关于向 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<MouseEvent>() {
            @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<Object>() {
            @Override
            public void changed(ObservableValue<?> observable, Object oldValue,
                                Object newValue) {
                // from stackoverflow.com/a/30264399/1032167
                // for some reason setScrollTop will not scroll properly
                //consoleTextArea.setScrollTop(Double.MAX_VALUE);
                  consoleTextArea.selectPositionCaret(consoleTextArea.getLength()); 
                  consoleTextArea.deselect(); 
            }
        });

        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

【讨论】:

  • 我有一个小错误,但我现在没有,如果它已链接,它不会到 textarea 的末尾,而是到 end-1 行
  • 我的错误来自于在我的文本区域之前绑定更新我的字符串
  • @EvansBelloeil 我认为这是 TextArea setText() 中的某种刷新错误,但我可能是错的。这篇文章是一个很好的解决方案:stackoverflow.com/a/30264399/1032167 只需添加 consoleTextArea.selectPositionCaret(consoleTextArea.getLength()); consoleTextArea.deselect(); 而不是 @987654326 @
  • 是的,就是这样,你能更新你的帖子,并解释错误吗?
猜你喜欢
  • 2011-11-08
  • 2013-07-21
  • 2011-02-12
  • 1970-01-01
  • 1970-01-01
  • 2012-10-20
  • 2016-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多