Java 将 JScrollPane 与 JPanel 滚动到底部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6131735/
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
Java scroll JScrollPane with JPanel within to bottom
提问by arik
I have a JScrollPane with a very high JPanel inside, that is changed dynamically, items being appended at its end. What I want, is to scroll to the bottom of aforementioned JScrollPane in order for the newly appended items to be visible instantly on addition (they are not appended to the scroll pane directly, but to its JPanel, and are private objects, so cannot be referenced.
我有一个 JScrollPane,里面有一个非常高的 JPanel,它是动态变化的,项目被附加到它的末尾。我想要的是滚动到上述 JScrollPane 的底部,以便新附加的项目在添加时立即可见(它们不是直接附加到滚动窗格,而是附加到它的 JPanel,并且是私有对象,所以不能参考。
How can I simply have that scroll pane scroll to the very bottom? Thanks in advance!
我怎么能简单地让滚动窗格滚动到最底部?提前致谢!
回答by Andrew Thompson
JComponent.scrollRectToVisible(Rectangle)
. Call that on the JPanel
instance.
JComponent.scrollRectToVisible(Rectangle)
. 在JPanel
实例上调用它。
E.G.
例如
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ScrollToNewLabel {
public static void main(String[] args) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
JPanel gui = new JPanel(new BorderLayout(3,3));
final JPanel panel = new JPanel(new GridLayout(0,1));
JScrollPane scroll = new JScrollPane(panel);
scroll.setPreferredSize(new Dimension(80,100));
gui.add(scroll, BorderLayout.CENTER);
JButton addLabel = new JButton("Add Label");
gui.add(addLabel, BorderLayout.NORTH);
ActionListener listener = new ActionListener() {
int counter = 0;
public void actionPerformed(ActionEvent ae) {
panel.add(new JLabel("Label " + ++counter));
panel.revalidate();
int height = (int)panel.getPreferredSize().getHeight();
Rectangle rect = new Rectangle(0,height,10,10);
panel.scrollRectToVisible(rect);
}
};
addLabel.addActionListener(listener);
JOptionPane.showMessageDialog(null, gui);
}
});
}
}
Screen shot
截屏
E.G. 2
EG 2
This e.g. is based on Vincent's answer, to use JScrollPane.getVerticalScrollBar()
.setValue(height)
. Where height
is the preferred height of the panel in pixels.
这例如基于文森特的回答,使用JScrollPane.getVerticalScrollBar()
. setValue(height)
. height
面板的首选高度(以像素为单位)在哪里。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ScrollToNewLabel {
public static void main(String[] args) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
JPanel gui = new JPanel(new BorderLayout(3,3));
final JPanel panel = new JPanel(new GridLayout(0,1));
final JScrollPane scroll = new JScrollPane(panel);
scroll.setPreferredSize(new Dimension(80,100));
gui.add(scroll, BorderLayout.CENTER);
JButton addLabel = new JButton("Add Label");
gui.add(addLabel, BorderLayout.NORTH);
ActionListener listener = new ActionListener() {
int counter = 0;
public void actionPerformed(ActionEvent ae) {
panel.add(new JLabel("Label " + ++counter));
panel.revalidate();
int height = (int)panel.getPreferredSize().getHeight();
scroll.getVerticalScrollBar().setValue(height);
}
};
addLabel.addActionListener(listener);
JOptionPane.showMessageDialog(null, gui);
}
});
}
}
回答by camickr
scrollRectToVisible(...) and scrollBar.setValue(...) are the general solutions.
scrollRectToVisible(...) 和 scrollBar.setValue(...) 是通用的解决方案。
You may be interested in Scrolling a Formwhich ensures that when you tab to a component the form will scroll automatically to make sure the the component will be visible in the scrollpane. Behind the scenes it uses scrollRectToVisible().
您可能对Scrolling a Form感兴趣,它确保当您 Tab 到一个组件时,表单将自动滚动以确保该组件在滚动窗格中可见。在幕后,它使用 scrollRectToVisible()。
回答by Matthias Braun
This is how I scroll down programmatically.
这就是我以编程方式向下滚动的方式。
I like how it scrolls to the bottom rather smoothly instead of jumping there immediately.
我喜欢它如何平滑地滚动到底部而不是立即跳到那里。
/**
* Scrolls a {@code scrollPane} to its bottom.
*
* @param scrollPane
* the scrollPane that we want to scroll all the way down
*
*/
private void scrollDown(JScrollPane scrollPane) {
JScrollBar verticalBar = scrollPane.getVerticalScrollBar();
int currentScrollValue = verticalBar.getValue();
int previousScrollValue = -1;
while (currentScrollValue != previousScrollValue) {
// Scroll down a bit
int downDirection = 1;
int amountToScroll = verticalBar.getUnitIncrement(downDirection);
verticalBar.setValue(currentScrollValue + amountToScroll);
previousScrollValue = currentScrollValue;
currentScrollValue = verticalBar.getValue();
}
}
回答by Vincent Ramdhanie
A simple way to move the scrollbar all the way to the bottom is to set its value to 100 like this:
将滚动条一直移动到底部的一种简单方法是将其值设置为 100,如下所示:
scroll.getVerticalScrollBar().setValue(100);
This causes it to move to the bottom of the viewport. You can add this after the component is added to the panel.
这会导致它移动到视口的底部。您可以在将组件添加到面板后添加它。