等到重新绘制组件完成

Leo*_*eon 2 java swing repaint

我有一个带有JPanel的ScrollPane,我在其中添加了大约200个组件JPanel.知道我希望Vertical-Slider移动到特定的Component.但问题是,当我打电话时:

myButton.scrollRectToVisible(myButton.getBounds());
Run Code Online (Sandbox Code Playgroud)

getBounds() 给我宽度和高度0.

显然,元素仍然没有被绘制.我怎么能等到repaint()方法完成它的所有工作,这样我才能做到scrollRectToVisible()

编辑:

如果我给出了我的代码的简短示例,它可能会有所帮助:

 addElementsToPanel(); //function where I add Elements to the Panel

 myJPanel.revalidate();
 myJPanel.repaint();

 myButton.scrollRectToVisible(myButton.getBounds());
Run Code Online (Sandbox Code Playgroud)

Sta*_*avL 5

SwingUtilities.invokeLater()