user1015830
user1015830

Reputation: 111

JScrollPane - automatically scroll to bottom?

I've been trying to create a a scroll pane that automatically keeps scrolling down as the user adds more lines of text to the window. I implemented this:

pane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
    public void adjustmentValueChanged(AdjustmentEvent e) {
        e.getAdjustable().setValue(e.getAdjustable().getMaximum());  
    }
});

"pane" is a JScrollPane that is holding a JList.

Unfortunately, the code above sticks the scroll bar to the bottom of the pane (which is what I wanted) but then I am unable to move the scroll bar from its position (so now it's stuck at the bottom). Is there a simple way to have the scroll bar stick to the bottom, but still be able to be scrolled elsewhere?

Thanks!

Upvotes: 3

Views: 2460

Answers (1)

camickr
camickr

Reputation: 324118

When you add an item to the JList you can use the method:

list.ensureIndexIsVisible(...)

where the index is the index of the last item in the list.

Upvotes: 7

Related Questions