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