JScrollPane and JList auto scroll
autoscroll, java, jlist, jscrollpane, swing
Solution
When adding a new message, invoke `scrollRectToVisible()` on the `JList` using a `Rectangle` having the same dimensions as your message pane's preferred size. Given a vertical orientation, it may be convenient to make the preferred size of the `JScrollPane`'s `JViewport` an integral multiple of the message pane's height. See also: How to Use Scroll Panes.
Addendum: This compelling discussion of Text Area Scrolling may be helpful, too.
Problem
I've got the next code: ``` listModel = new DefaultListModel(); listModel.addElement(dateFormat.format(new Date()) + ": Msg1"); messageList = new JList(listModel); messageList.setLayoutOrientation(JList.VERTICAL); messageScrollList = new JScrollPane(messageList); messageScrollList.setPreferredSize(new Dimension(500, 200)); messageScrollList.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent e) { e.getAdjustable().setValue(e.getAdjustable().getMaximum()); } }); ``` It auto scrolls down. But, if I try to scroll back up to re-read a message, it forces a scroll down. How can I fix this?