TextArea setScrollTop

时间:2014-05-06 13:41:26

标签: javafx javafx-2 javafx-8

我创建了TextArea的这个简单示例

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顶部的位置。 你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

如果您在显示舞台后设置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); 
}
相关问题