문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top