문제

I created this simple example of 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);
    }

}

I want to set by default the the position of the slider to be at the top of the TextArea. Can you help me to fix this?

도움이 되었습니까?

해결책

If you set dataPane.setScrollTop(0); after your stage is shown, it will work :)

So like this:

@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); 
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top