Carven
Carven

Reputation: 15660

Getting SWT StyledText widget to always scroll to its end

How do I get a SWT StyledText widget to always stay scrolled to the end it even as new lines of text gets appended to it?

I tried to look for some functions that could allow me to set the scroll position but I can't find any. There isn't a property that lets me do this either.

Upvotes: 3

Views: 7395

Answers (2)

Gihad Murad
Gihad Murad

Reputation: 31

Another variation:

    styledText.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent e) {
            styledText.setTopIndex(styledText.getLineCount() - 1);

        }
    });

Upvotes: 3

the.duckman
the.duckman

Reputation: 6406

Simply add this line, after you've added text:

styledText.setTopIndex(styledText.getLineCount() - 1);

If you change the content of your StyledText on more than one place, use a listener on Modify, to not repeat yourself:

styledText.addListener(SWT.Modify, new Listener(){
    public void handleEvent(Event e){
        styledText.setTopIndex(styledText.getLineCount() - 1);
    }
});

Upvotes: 10

Related Questions