Java – JScrollPane – auto scroll to bottom?
•
Java
I've been trying to create a scrolling pane that automatically scrolls down when users add more lines of text to the window I achieved this:
pane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent e) { e.getAdjustable().setValue(e.getAdjustable().getMaximum()); } });
"Pane" is a JScrollPane that holds JList
Unfortunately, the above code pastes the scroll bar to the bottom of the pane (which is what I want), but I can't move the scroll bar from its position (so now it's stuck at the bottom) Is there a simple way to stick the scroll bar to the bottom, but still scroll to other places?
thank you!
Solution
When adding items to JList, you can use the following methods:
list.ensureIndexIsVisible(...)
Where index is the index of the last item in the list
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码