epn
epn

Reputation: 83

How to Disable TextArea Auto Scroll in JavaFX and Java 8?

I want to disable the Auto Scrolling of TextArea in javaFx, Because when I add text to TextArea the scroll is moving down, I don't want to Auto Scroll the Scroll bar in TextArea, if I add the text scroll should be selected position.(Example if user is reading middle line if add the text it Automatically going to down, So again user need to move the scroll to middle line). So for that reason I want to disable the Auto Scroll in TextArea in JavaFx.

javafx.application.Platform.runLater( new Runnable() {
  @Override
  public void run() {
  logTextArea.appendText("I am adding text here to TextArea"+"\n");// Adding the text to logTextArea
   }
});

Below are the ref links :- please check this link

Please Check this link also

Upvotes: 1

Views: 3447

Answers (1)

saeid rastak
saeid rastak

Reputation: 353

From How can I hide the scroll bar in TextArea?:

Remove Horizontal Scrollbar

textArea.setWrapText(true);

Remove Vertical Scrollbar

ScrollBar scrollBarv = (ScrollBar)ta.lookup(".scroll-bar:vertical");
scrollBarv.setDisable(true);

CSS

.text-area .scroll-bar:vertical:disabled {
    -fx-opacity: 0;
}

Example:

public class Test2 extends Application {

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

@Override
public void start(Stage primaryStage) {
    TextArea textArea = new TextArea("This is my message");
    textArea.setWrapText(true);
    String css = this.getClass().getResource("/ta.css").toExternalForm();
    textArea.getStylesheets().add(css);
    primaryStage.setScene(new Scene(textArea));
    primaryStage.show();
    ScrollBar scrollBar = (ScrollBar) textArea.lookup(".scroll-bar:vertical");
    scrollBar.setDisable(true);

}

}

Upvotes: 1

Related Questions