Cannot programmatically scroll to last item in JList

I have a JList nested inside a JScrollPane. When I add elements to the JList, I want the JScrollPane to automatically scroll the bottom of the JList so that the last element is visible. For this, I have the following code:

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

However, when I try to use this code, JScrollPane only scrolls the second to the last element, leaving the last element out of sight. This is in no way desirable. I tried adding values ​​to getMaximum (), but the problem persists.

How can I get JScrollPane to scroll to the bottom?

+5
source share
2 answers

JList # secureIndexIsVisible():

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

, : http://forums.sun.com/thread.jspa?threadID=623669 ( "inopia" ) .

: " , , ListModel, JList JScrollPane".

0

All Articles