Scroll JTextArea Based on the Top Visible Line
I am having trouble with getting a JTextArea to scroll to a certain position in the middle of the text.
I had:
textArea.setCaretPosition(index);
The problem is that that displayed the desired line at the bottom of the visible area. I want to scroll so that it is at the top of the visible area. (My text area is of variable height and width, so I can't just add a constant to make it right.)
I tried this:
textArea.scrollRectToVisible(new Rectangle(0,
textArea.getLineOfOffset(index)
* (textArea.getHeight() / numberOfLines),
1,
1));
and this:
textArea.scrollRectToVisible(new Rectangle(0,
textArea.getLineOfOffset(index)
* textArea.getFont().getSize(),
1,
1));
but neither of those seems to do anything.

