JScrollPane和JList自动滚动 [英] JScrollPane and JList auto scroll

查看:180
本文介绍了JScrollPane和JList自动滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一个代码:

    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?

推荐答案

添加新消息时,调用 scrollRectToVisible() <$ c的c $ c> 使用 JList noreferrer> 矩形 ,其尺寸与消息窗格的首选大小相同。给定垂直方向,可以方便地设置 JScrollPane JViewport 是消息窗格高度的整数倍。另请参阅: 如何使用滚动窗格

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.

这篇关于JScrollPane和JList自动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆