Frage

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

War es hilfreich?

Lösung

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);
 }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top