JScrollPane and JList auto scroll

后端 未结 7 1610
无人共我
无人共我 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:40

    @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.

    0 讨论(0)
提交回复
热议问题