0

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

Valkyrie
  • 108
  • 13
  • The units for the scroll bar are pixels not character offsets. The units for `setTopIndex` are lines. – greg-449 Dec 18 '13 at 10:20

1 Answers1

0

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);
 }
Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
  • How do you keep the styledText from reseting the setTopIndex to 0 everytime? – Valkyrie Dec 19 '13 at 06:35
  • Not understood, add some more explanation – Chandrayya G K Dec 19 '13 at 06:52
  • everytime the .setText method is called, the .setTopIndex method gets called automatically with the value 0. I was wondering on how to stop this from happening, because it causes the text in the area to jump and look jittery – Valkyrie Dec 19 '13 at 07:21
  • I got rid of the jumping of the text. It looks a lot smoother if you disable redraw, set the text and topIndex and then enable redraw again. If you have a better solution, I would like to hear it though – Valkyrie Dec 19 '13 at 07:38
  • You can try calling styledText.append() if you want to append text also check other methods of StyledText these may help or post the complete code – Chandrayya G K Dec 19 '13 at 10:09