Set the vertical scroll to current position after

2019-09-06 07:24发布

I know that this question might have been asked before, but I just can't get by head around this, and hopefully we could produce a complete answer to a somewhat tricky interface.

The GUI could be described as follows: Application extends JFrame. Application adds a JPanel mPanel. mPanel adds a JScrollPane ml containing a MoviePanel extending JPanel.

The JScrollPane ml has vertical scrolling. My goal is that once the content of MoviePanel changes, and a run a revalidate() on it, the scroll pane should not, as it currently does, scroll to the bottom. Rather I'd like it to scroll to what ever position it had before the change to MoviePanel. Giving the feel that it never scrolled at all.

I have tried to manually set the scroll position after I run the revalidate() method:

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

but it seems it really doesn't do anything.

I would be so grateful if someone might help me with this!

1条回答
霸刀☆藐视天下
2楼-- · 2019-09-06 07:29

Add the scrolling code to a SwingUtilities.invokeLater:

SwingUtilities.invokeLater(new Runnable()
{
    @Override
    public void run()
    {
        ml.getVerticalScrollBar().setValue(0);
    }
});
查看更多
登录 后发表回答