Scrolling SWT scrollBar

344 views Asked by At

I have a SWT styledText area with scrollBar. I would like the scrollbar to scroll to a given index but I am experiencing some difficulties.

I tried:

ScrollBar vBarScroll = styledText.getVerticalBar();
vBarScroll.setSelection(textIndex);
styledText.showSelection();

This selects the text, which I do not want, as well as having difficulties to display. It jumops around alot too.

The other thing I tried is:

styledText.setTopIndex(textIndex);

But that doesn't work either

1

There are 1 answers

5
Chandrayya G K On BEST ANSWER

Try this:

lineNoIndex is the index of line. 0 -> 1st line 1->Second line etc.

 if(lineNoIndex >= 0 && lineNoIndex < styledText.getLineCount()) {
       int offset = lineNoIndex == 0 ? 0:styledText.getOffsetAtLine(lineNoIndex) - 1;
       styledText.setCaretOffset(offset);
       styledText.setTopIndex(offset == 0 ? 0:lineNoIndex - 1);
 }