0

我正在制作一个JTextPane带有 html 功能的聊天室。用户可以输入 html 标签以在屏幕上显示图像。但是我在将滚动条保持在底部时遇到了问题。我已经尝试做

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        vertical.setValue(vertical.getMaximum());
    }
});

但滚动条向下滚动然后再次向上滚动。似乎在调用函数后图片完成加载。我也试过:

ClientScreen._chatMsgPane.setCaretPosition(_chatMsgPane.getDocument().getLength());

但结果是一样的。所有图像加载完成后是否会触发任何事件?或者有没有其他方法可以解决这个问题?

4

2 回答 2

0

JComponent has a method named scrollRectToVisible which is designed to do this:

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        int end = chatMsgPane.getDocument().getLength();
        try {
            chatMsgPane.scrollRectToVisible(chatMsgPane.modelToView(end));
        } catch (BadLocationException e) {
            throw new RuntimeException(e); // Should never get here.
        }
    }
});

I'm not entirely sure how you are addng the image to the JTextPane, but if you are loading it yourself, you can load it in a different thread with ImageIO.read and then scroll to bottom when the read is finished; or, if you want to show the image progressively, you can obtain an ImageReader from ImageIO, and pass your own listener to its addIIOReadProgressListener method.

于 2013-06-10T18:06:01.717 回答
0

我从未在加载图像的 JTextPane 上尝试过此操作,但智能滚动可能对您有用。

于 2013-06-10T02:15:47.620 回答