开发者

Need Java Swing equivalent of "scrollIntoView" from Browser DOM

I have a JPanel with several levels of child components, a开发者_如何学Golso 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 scrollRectToVisible on is the JScrollPane, which will compile fine but won't do what you want. You must call scrollRectToVisible on the object contained in viewport of the scrollpane. The code should look sort of like:

java.awt.Component focusedComponent = evt.getComponent();
panel.scrollRectToVisible(focusedComponent.getBounds(null));
repaint();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜