gpt4 book ai didi

java - 无法以编程方式滚动到 JList 中的最后一项

转载 作者:搜寻专家 更新时间:2023-10-31 08:18:24 26 4
gpt4 key购买 nike

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

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

但是,当我尝试使用此代码时,JScrollPane 仅滚动到倒数第二个项目,而最后一个项目不在 View 中。这无论如何都不可取。我已尝试向 getMaximum() 添加值,但问题仍然存在。

如何让 JScrollPane 滚动到最底部?

最佳答案

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

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

关于java - 无法以编程方式滚动到 JList 中的最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2695767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com