我有一个 JList 嵌套在 JScrollPane 中。当我向 JList 添加项目时,我希望 JScrollPane 自动滚动到 JList 的底部,以便最后一个项目可见。为此,我有以下代码:
getWordListScroller().getVerticalScrollBar().getModel().setValue(getWordListScroller().getVerticalScrollBar().getModel().getMaximum());
然而,当我尝试使用此代码时,JScrollPane 仅滚动到倒数第二个项目,使最后一个项目不可见。这无论如何都是不可取的。我尝试向 getMaximum() 添加值,但问题仍然存在。
如何让 JScrollPane 滚动到最底部?
尝试使用JList#ensureIndexIsVisible() http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JList.html#ensureIndexIsVisible%28int%29 method:
JList wordList = getWordListScroller ();
int lastIndex = wordList.getModel().getSize() - 1;
if (lastIndex >= 0) {
wordList.ensureIndexIsVisible(lastIndex);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)