Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JTextPane inside a JScrollPane: any way to display a certain section?

843806Jan 3 2009 — edited Jan 25 2009
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?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 22 2009
Added on Jan 3 2009
23 comments
839 views