如何在 Java Swing 中自动滚动到底部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6379061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:40:01  来源:igfitidea点击:

How to auto scroll to bottom in Java Swing

javaswingscrolljpaneljscrollpane

提问by pathikrit

I have a simple JPanel with a JScrollPane (with vertical scrollbar as needed) on it.

我有一个简单的 JPanel,上面有一个 JScrollPane(根据需要带有垂直滚动条)。

Things get added to (or removed from) the JPanel and when it goes beyond the bottom of the panel, I want the JScrollPane to scroll down to the bottom automatically as needed or scroll up if some components go away from the panel.

东西被添加到(或从)JPanel 中删除,当它超出面板底部时,我希望 JScrollPane 根据需要自动向下滚动到底部,或者如果某些组件离开面板,则向上滚动。

How shall I do this? I am guessing I need some kind of listener which gets called whenever the JPanel height changes? Or is there something as simple as JScrollPanel.setAutoScroll(true)?

我该怎么做?我猜我需要某种监听器,它会在 JPanel 高度发生变化时被调用?或者有这么简单的事情JScrollPanel.setAutoScroll(true)吗?

采纳答案by camickr

When you add/remove components for a panel you should invoke revalidate() on the panel to make sure the components are laid out properly.

当您为面板添加/删除组件时,您应该调用面板上的 revalidate() 以确保组件布局正确。

Then, if you want to scroll to the bottom then you should be able to use:

然后,如果你想滚动到底部,那么你应该能够使用:

JScrollBar sb = scrollPane.getVerticalScrollBar();
sb.setValue( sb.getMaximum() );

回答by zerodefect

scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {  
        public void adjustmentValueChanged(AdjustmentEvent e) {  
            e.getAdjustable().setValue(e.getAdjustable().getMaximum());  
        }
    });

This would be the best. Found from JScrollPane and JList auto scroll

这将是最好的。从JScrollPane 和 JList 自动滚动中找到

回答by Matthias Braun

This is how I scroll all the way up or down automatically:

这就是我自动向上或向下滚动的方式:

/**
 * Scrolls a {@code scrollPane} all the way up or down.
 *
 * @param scrollPane the scrollPane that we want to scroll up or down
 * @param direction  we scroll up if this is {@link ScrollDirection#UP},
 *                   or down if it's {@link ScrollDirection#DOWN}
 */
public static void scroll(JScrollPane scrollPane, ScrollDirection direction) {
    JScrollBar verticalBar = scrollPane.getVerticalScrollBar();
    // If we want to scroll to the top, set this value to the minimum,
    // else to the maximum
    int topOrBottom = direction == ScrollDirection.UP ?
                      verticalBar.getMinimum() :
                      verticalBar.getMaximum();

    AdjustmentListener scroller = new AdjustmentListener() {
        @Override
        public void adjustmentValueChanged(AdjustmentEvent e) {
            Adjustable adjustable = e.getAdjustable();
            adjustable.setValue(topOrBottom);
            // We have to remove the listener, otherwise the
            // user would be unable to scroll afterwards
            verticalBar.removeAdjustmentListener(this);
        }
    };
    verticalBar.addAdjustmentListener(scroller);
}

public enum ScrollDirection {
    UP, DOWN
}