In an application with Java Swing components I have a JTextPane
inside a non-editable JScrollPane
, and some other fields where the user can enter some criteria. If the user clicks a certain button or menu item, there will be a search for a section of the document in the JTextPane
that meets the given criteria, and if a match is found the section will be highlighted.
That part is solved; I can find the relevant document segment, know where it starts and ends (in terms of integer offsets), and highlight it.
But that highlighted segment might have scrolled off the screen and I need it to be scrolled to within the visible piece of the JScrollPane. I see there is a method scrollRectToVisible(Rectangle)
on JComponent
that seems like it might to do the job, but I don't see how to convert from the textual document position integer to a Rectangle
.
Note that it is a non-editable JTextPane and I don't want to move the cursor the highlighted segment (the user cannot see the cursor anyway), I only want to make the segment show within the visible area of the JScrollPane.