JScrollPane - 自动滚动到底部?
我一直在尝试创建一个滚动窗格,当用户向窗口添加更多文本行时,它会自动向下滚动。我实现了这个:
pane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
e.getAdjustable().setValue(e.getAdjustable().getMaximum());
}
});
“pane”是一个持有 JList 的 JScrollPane。
不幸的是,上面的代码将滚动条粘贴到窗格的底部(这就是我想要的),但随后我无法将滚动条从其位置移动(所以现在它卡在底部)。有没有一种简单的方法可以让滚动条粘在底部,但仍然能够滚动到其他地方?
谢谢!
I've been trying to create a a scroll pane that automatically keeps scrolling down as the user adds more lines of text to the window. I implemented this:
pane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
e.getAdjustable().setValue(e.getAdjustable().getMaximum());
}
});
"pane" is a JScrollPane that is holding a JList.
Unfortunately, the code above sticks the scroll bar to the bottom of the pane (which is what I wanted) but then I am unable to move the scroll bar from its position (so now it's stuck at the bottom). Is there a simple way to have the scroll bar stick to the bottom, but still be able to be scrolled elsewhere?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您向 JList 添加项目时,可以使用以下方法:
其中索引是列表中最后一项的索引。
When you add an item to the JList you can use the method:
where the index is the index of the last item in the list.