HTML 的 JScrollBar + JTextPane 没有正确滚动到最大值

JScrollBar + JTextPane with HTML not properly scrolling to maximum value

我在我的一个项目中遇到了以下问题,我花了一段时间才弄清楚是什么导致了这个问题,我可以用我附加的这个简单代码重现它。

我正在使用 HTMLEditorKit 向 JTextPane 动态添加内容。我将自动滚动设置为关闭,因为我想手动控制它(当用户向上滚动时停止,以及当触发事件时再次激活)。

现在的问题是,当我将 JScrollBar 的值设置为其最大值时,它是一个不同的值,就在将内容插入到 HTMLDocument 之后。当我再次手动再次触发 setValue 时,它​​会滚动到正确的最大值。

似乎 JScrollBar 在添加到 HTMLDocument 之后并不知道正确的 maximumValue,只是(延迟)了一段时间。

正在使用

caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

不是解决方案,因为它也无法正常工作。它也不会滚动到最大值,在下方留下一个视图像素,这是我不想要的。

这是重现问题的完整代码。如果您单击右键(添加并滚动),它会向主体插入一个 DIV 元素。当到达最后一条可见线时,它没有正确滚动到最后一条最大值,最后一条线被隐藏。但是当你手动点击左键触发第二个scrollToEnd()时,它会正确滚动到最大值

代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication26;

import java.io.IOException;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;
import javax.swing.text.Element;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

public class NewJFrame extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {

        initComponents();

        this.setSize(500, 200);
        this.setLocationRelativeTo(null);

        this.jTextPane1.setEditorKit(new HTMLEditorKit());
        this.jTextPane1.setContentType("text/html");

        this.jTextPane1.setText("<html><body><div id=\"GLOBALDIV\"></div></body></html>");

        this.jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        this.jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

        DefaultCaret caret = (DefaultCaret) this.jTextPane1.getCaret();
        caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);

        this.jScrollPane1.setAutoscrolls(false);
        this.jTextPane1.setAutoscrolls(false);
    }

    private void scrollToEnd() {

        this.jScrollPane1.getVerticalScrollBar().setValue(this.jScrollPane1.getVerticalScrollBar().getMaximum());
        //this.jTextPane1.setCaretPosition(this.jTextPane1.getDocument().getLength());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setViewportView(jTextPane1);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jButton1.setText("Scroll to end");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton1);

        jButton2.setText("Add & scroll");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton2);

        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        try {

            HTMLDocument doc = (HTMLDocument) this.jTextPane1.getDocument();
            HTMLEditorKit editorKit = (HTMLEditorKit) this.jTextPane1.getEditorKit();

            SecureRandom random = new SecureRandom();
            String htmlCode = "<div style=\"background-color: #FFFF22; height: 12px; font-size: 12;\">"+new BigInteger(64, random).toString(64)+"</div>";

            //editorKit.insertHTML(doc, doc.getLength(), htmlCode, 0, 0, null);
            Element element = doc.getElement("GLOBALDIV");

            if (element != null) {
                doc.insertBeforeEnd(element, htmlCode);
            }

            this.scrollToEnd();
        } catch (BadLocationException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        this.scrollToEnd();
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane jTextPane1;
    // End of variables declaration                   
}

此代码替换虽然有效,但留下了一个小间隙,也无法正确滚动到最大值:

this.jTextPane1.setCaretPosition(0);
this.jTextPane1.setCaretPosition(this.jTextPane1.getDocument().getLength());

当您将 div 插入文档时,文档模型会立即更新。但是JTextPane只收到无效的通知,需要布局。此通知在 EDT 上创建一个事件,该事件仅在当前事件(由单击的按钮触发)完成后处理。

因此,在您调用scrollToEnd()的那一刻,JTextPane的重新生效仍在等待中,文本窗格的高度仍然太小。

为了获得正确的事件顺序,您需要使用 invokeLater:

在 EDT 中安排 scrollToEnd() 的调用
SwingUtilities.invokeLater(new Runnable(){
    public void run(){
         scrollToEnd();
    }
});