I have a JPanel with several levels of child components, also with a JScrollPane. I’m placing a focus listener on some of the child components to add some behavior to those components, but I would also like to have that component scroll into the JPanel’s viewport when focus is gained.
My question is, does anyone have a general purpose function to do this, similar to the browser DOM function “scrollIntoView”? I’ve tried muddling through this with various inputs to JComponent.scrollRectToVisible but I guess I haven’t figured out the magic word.
Thanks in advance.
The obvious thing is to call
scrollRectToVisibleon is the JScrollPane, which will compile fine but won’t do what you want. You must callscrollRectToVisibleon the object contained in viewport of the scrollpane. The code should look sort of like: