2014-09-23 41 views
0

我知道这个问题以前可能已经被问过了,但我无法通过解决这个问题,并希望我们能够为一个有点棘手的接口提供完整的答案。在重新验证之后将垂直滚动设置为当前位置

GUI可以描述如下: 应用程序扩展了JFrame。应用程序添加一个JPanel mPanel。 mPanel添加一个包含扩展JPanel的MoviePanel的JScrollPane ml。

JScrollPane ml具有垂直滚动功能。我的目标是,一旦MoviePanel的内容发生变化,并在其上运行revalidate(),滚动窗格就不应该像当前那样滚动到底部。相反,我希望它能够在改变到MoviePanel之前滚动到它所处的位置。赋予它永远不会滚动的感觉。

我试图手动设置滚动位置我运行重新验证()方法后:

removeAll(); // Removes all components from the JPanel MoviePanel 
add(mList()); // Adds a bunch of content (other JPanels) to MoviePanel 
revalidate(); 
ml.getVerticalScrollBar().setValue(0); // Scroll to top (don't work) - and I'd like this value to be the position of the scroll before these lines started to run 

但似乎真的没有做任何事情。

如果有人能帮助我,我会很感激!

回答

0

滚动代码添加到SwingUtilities.invokeLater:

SwingUtilities.invokeLater(new Runnable() 
{ 
    @Override 
    public void run() 
    { 
     ml.getVerticalScrollBar().setValue(0); 
    } 
}); 
相关问题