文本区setScrollTop(TextArea setScrollTop)

2019-10-20 02:18发布

我创建文本区的这个简单的例子

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class MainApp extends Application
{

    @Override
    public void start(Stage stage) throws Exception
    {

        HBox hbox = new HBox();
        // Text Area
        TextArea dataPane = new TextArea();
        dataPane.setScrollTop(0);
        dataPane.setEditable(false);
        dataPane.prefWidthProperty().bind(hbox.widthProperty());

        dataPane.setWrapText(true);     // New line of the text exceeds the text area
        dataPane.setPrefRowCount(10);
        dataPane.appendText("Computer software, or simply software, also known as computer programs");
        dataPane.appendText("\nis the non-tangible component of computers.");
        dataPane.appendText("\nComputer software contrasts with computer hardware, which");
        dataPane.appendText("\nis the physical component of computers.");
        dataPane.appendText("Computer hardware and software require each");
        dataPane.appendText("\nother and neither can be");
        dataPane.appendText("\nrealistically used without the other.");
        dataPane.appendText("\nComputer software");

        hbox.setAlignment(Pos.CENTER);
        hbox.setSpacing(1);
        hbox.setPadding(new Insets(10, 0, 10, 0));
        hbox.getChildren().add(dataPane);

        Scene scene = new Scene(hbox, 800, 90);

        stage.setTitle("JavaFX and Maven");
        stage.setScene(scene);
        stage.show();
    }

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

}

我想在默认情况下滑块的位置来设定在TextArea的顶部。 你能帮我解决这一问题?

Answer 1:

如果设置dataPane.setScrollTop(0); 显示你的舞台后,它会工作:)

所以像这样的:

@Override
public void start(Stage stage) {
    HBox hbox = new HBox();
    // Text Area
    TextArea dataPane = new TextArea();

    dataPane.setEditable(false);
    dataPane.prefWidthProperty().bind(hbox.widthProperty());

    dataPane.setWrapText(true);     // New line of the text exceeds the text area
    dataPane.setPrefRowCount(10);
    dataPane.appendText("Computer software, or simply software, also known as computer programs");
    dataPane.appendText("\nis the non-tangible component of computers.");
    dataPane.appendText("\nComputer software contrasts with computer hardware, which");
    dataPane.appendText("\nis the physical component of computers.");
    dataPane.appendText("Computer hardware and software require each");
    dataPane.appendText("\nother and neither can be");
    dataPane.appendText("\nrealistically used without the other.");
    dataPane.appendText("\nComputer software");

    hbox.setAlignment(Pos.CENTER);
    hbox.setSpacing(1);
    hbox.setPadding(new Insets(10, 0, 10, 0));
    hbox.getChildren().add(dataPane);

    Scene scene = new Scene(hbox, 800, 90);

    stage.setTitle("JavaFX and Maven");
    stage.setScene(scene);
    stage.show();

    dataPane.setScrollTop(0.0); 
}


文章来源: TextArea setScrollTop