重新验证后,将垂直滚动条设置到当前位置 [英] Set the vertical scroll to current position after revalidate

查看:246
本文介绍了重新验证后,将垂直滚动条设置到当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道以前可能会问过这个问题,但是我无法直截了当地解决这个问题,希望我们可以对有些棘手的界面给出完整的答案.

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.

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

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.

JScrollPane ml具有垂直滚动.我的目标是,一旦更改MoviePanel的内容并对其运行revalidate(),滚动窗格就不应像当前那样滚动到底部.相反,我希望它滚动到更改为MoviePanel之前的位置.给人一种它从未滚动过的感觉.

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.

运行revalidate()方法后,我试图手动设置滚动位置:

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!

推荐答案

将滚动代码添加到SwingUtilities.invokeLater:

Add the scrolling code to a SwingUtilities.invokeLater:

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

这篇关于重新验证后,将垂直滚动条设置到当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆