JScrollPane and JList auto scroll

后端 未结 7 1614
无人共我
无人共我 2020-12-16 14:33

I\'ve got the next code:

    listModel = new DefaultListModel();
    listModel.addElement(dateFormat.format(new Date()) + \": Msg1\");
    messageList = new          


        
7条回答
  •  感情败类
    2020-12-16 15:38

    I used JScrollPane with JTextArea. I avoided force scroll down by scrolling only when JScrollBar max value changed:

    JScrollPane scrollPane = new JScrollPane(jTextArea);
    
    verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum();
    scrollPane.getVerticalScrollBar().addAdjustmentListener(
        e -> {
            if ((verticalScrollBarMaximumValue - e.getAdjustable().getMaximum()) == 0)
                return;
            e.getAdjustable().setValue(e.getAdjustable().getMaximum());
            verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum();
        }
    );
    

    I suppose, there is better solution to filter events without extra variables, and would appreciate if somebody post it.

提交回复
热议问题