I have a GUI with a JTextPane inside a JScrollPane.
The text of the JTextPane is the contents of a text file, so it typically has many lines.
I want the JScrollPane (whose preferred size has already been previously set to display 11 rows of text) to always show one target line as the middle row, and the 5 rows before and after it as the remaining 10 rows.
In other words, I want to show a certain line and its surrounding context.
Do you guys know how to achieve this? I have already tried calling
JTextPane.setCaretPosition
but this is inadequate because it merely guarantees that the line with the caret is visible--but it does not guarantee to center the caret line in the middle of the JScrollPane.
I also tried calling
textPane.scrollRectToVisible( textPane.modelToView(caretPos) );
where textPane is my JTextPane, but this failed completely (seems to always display the final lines of the file).
Anyone have any suggestions?