带有HTML的JScrollBar + JTextPane无法正确滚动到最大值

时间:2017-03-28 10:41:58

标签: java html jtextpane jscrollbar htmleditorkit

我在我的一个项目中遇到以下问题,花了一些时间来弄清楚导致问题的原因,我可以用我附上的这个简单代码重现它。

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

现在的问题是,当我将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());

1 个答案:

答案 0 :(得分:1)

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

因此,在您调用scrollToEnd()时,JTextPane的重新验证仍处于待处理状态,文本窗格的高度仍然太小。

为了使事件序列正确,您需要使用invokeLater在EDT中安排scrollToEnd()的调用:

SwingUtilities.invokeLater(new Runnable(){
    public void run(){
         scrollToEnd();
    }
});