无法以编程方式滚动到 JList 中的最后一项 [英] Unable to programatically scroll to last item in JList

查看:48
本文介绍了无法以编程方式滚动到 JList 中的最后一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套在 JScrollPane 中的 JList.当我向 JList 添加项目时,我希望 JScrollPane 自动滚动到 JList 的底部,以便最后一个项目可见.为此,我有以下代码:

I have a JList nested inside of a JScrollPane. When I add items to the JList, I want the JScrollPane to automatically scroll to the bottom of the JList so the last item is visible. To do this, I have the following code:

getWordListScroller().getVerticalScrollBar().getModel().setValue(getWordListScroller().getVerticalScrollBar().getModel().getMaximum());

但是,当我尝试使用此代码时,JScrollPane 仅滚动到倒数第二个项目,而将最后一个项目隐藏在视图之外.这绝不是可取的.我已尝试向 getMaximum() 添加值,但问题仍然存在.

When I try using this code, however, the JScrollPane only scrolls to the second to last item, leaving the last item out of view. This is not in any way desirable. I've tried adding values to getMaximum(), but the issue persists.

如何让 JScrollPane 滚动到最底部?

How can I get the JScrollPane to scroll to the very bottom?

推荐答案

尝试使用 JList#ensureIndexIsVisible() 方法:

JList wordList = getWordListScroller ();
int lastIndex = wordList.getModel().getSize() - 1;
if (lastIndex >= 0) {
   wordList.ensureIndexIsVisible(lastIndex);
}

这篇关于无法以编程方式滚动到 JList 中的最后一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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