0

I have a program that prints out loads of text to the text area which i used as a console in my app.

It outputs live building information.

By default it scrolls down to the bottom which is nice, but i would like to be able to grab the scrolling bar and go up in text area to check what is going on while the text is still appending to the text area.

My question is how i can disable auto-scrolling when i grab the scrolling bar ?

The ideal scenario would be : Auto-scroll if the scrolling bar is way back to the end Disable auto-scrolling when i took the scrolling bar up as its difficult to read anything while the text jump back to the end all the time.

This is my text area appender

private void run(final TextArea console) {
    try {
        console.clear();
        BufferedReader stdInput;
        stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line;
        while ((line = stdInput.readLine()) != null) {
            String feed = line;
            Platform.runLater(() -> console.appendText(feed + "\n"));
        }
    } catch (Exception e) {
        console.appendText(e.toString());
    }
}
LazerBanana
  • 6,865
  • 3
  • 28
  • 47

1 Answers1

1

I had the same problem, and this solved it. Basically taking the scroll position before/after the text update and making sure it's the same:

Platform.runLater(() -> {
    double pos = console.getScrollTop();
    int anchor = console.getAnchor();
    int caret = console.getCaretPosition();
    console.appendText(feed + '\n');
    console.setScrollTop(pos);
    console.selectRange(anchor, caret);
});

Note: This is tested with .setText(), but not with appendText()

EDIT: Added selection code - without it, it will have undefined behavior

Josh Larson
  • 825
  • 10
  • 14
  • Thank you for your answer, I have done something similar but it felt wrong to do it, I have quite a lot happening in the console then this solution harm the performance a little. I thought I might be able to disable the autoscrolling when clicked on the anchor and enable it back after, but seems hard to find/do this – LazerBanana Mar 16 '17 at 08:52