Set Caret position with JTextArea in JScrollPane

Posted by Albinoswordfish on Stack Overflow See other posts from Stack Overflow or by Albinoswordfish
Published on 2011-01-07T21:54:05Z Indexed on 2011/01/08 6:53 UTC
Read the original article Hit count: 220

Filed under:

Right now I have a JTextArea inside of a JScrollPane. For the current content it has both a vertical and horizontal scroll bar showing up. I'm trying to implement a search functionality where a user can search for a certain string and it will set the caret position to the first occurrence of that string. However it seems that JScrollPane only scrolls vertically when I set my caret position. So matching strings going off the JTextArea horizontally will completely get missed and the horizontal scroll bar won't scroll at all.

I'm using the basic function setCaretPosition() for the JTextArea

Does anybody have any idea why my JScrollPane isn't moving horizontally using setCaretPosition()

Edit:

It appears the horizontal scroll bar is scrolling but it moves so little that it's barely noticeable. I can only see the very first pixel of the character. Is there a way to have the scrollbar center (or as much as possible) to the caret position?

© Stack Overflow or respective owner

Related posts about java