I\'ve got the next code:
listModel = new DefaultListModel();
listModel.addElement(dateFormat.format(new Date()) + \": Msg1\");
messageList = new
@question enquirer. Please change your code from
messageScrollList.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
e.getAdjustable().setValue(e.getAdjustable().getMaximum());
}
});
to
messageScrollList.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
e.getAdjustable().setValue(e.getAdjustable().getValue());
}
});
That is change getMaximum()
to getValue()
Then it works as required. getMaximum()
takes the scroller to its maximum; while getValue()
takes it wherever event happens.
You can avoid using this piece of code altogether if you put one line
messageScrollList.setViewportView(messageList);
That works like add(component)
for jList and gets its inherent behaviour.