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 o开发者_运维技巧f 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?
You should be able to use the Visible Caret Listener.
Or you can also look at Center Line in Scroll Pane. It only centers the line vertically, but you could customize the code to do horizontal as well.
精彩评论