简体   繁体   中英

How to disable textArea auto-scrolling in javaFx java8

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());
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM