C graphics
C graphics

Reputation: 7458

Writing to JEditorPane fast

In the code below I would like to write onto JEditorPane the numbers 0 to 10000. However, the JEditorPane does not display anything unless either it is completely done ( printing all 0 to 10000 at once) or when the os gives it time to refresh and display the contents. Either case, the application becomes unresponsive for a while that the user thinks the application has crashed. Is there anyway we can force the jEditorPane to display the updated contents, even if it is still busy ? I tried invokeAndWait but does not help here as we cannot call invokeAndWait from the event dispatcher thread.

In other words, if I replace the Thread.sleep(0) with Thread.sleep(50), it works fine and displays the print of results as they happen, but adding the 50 mill sec delay makes it extremely slow. all I want is to update the JEditorPane from time to time so that the user does not think the application crashed. I prefer to only modify the updateMessages(). Here is my code:

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

public class CMessageWindowStack {

    private static final String MESSAGE = "msg";
    private  JScrollPane scrollPane;
    public  JEditorPane  editorPane;
    private  HTMLEditorKit kit;
    private  String msgBuffer=new String("");
    private static CMessageWindowStack window=null;
    private static JFrame frameContainer=null;

    private CMessageWindowStack()
    {
        editorPane  = new JEditorPane ();
        editorPane.setEditable(false);
        editorPane.setContentType("text/html");
        kit = new HTMLEditorKit();
        editorPane.setEditorKit(kit);

        StyleSheet styleSheet = kit.getStyleSheet();
        styleSheet.addRule("."+MESSAGE+" {font: 10px monaco; color: black; }");

        Document doc = kit.createDefaultDocument();
        editorPane.setDocument(doc);
        scrollPane = new JScrollPane(editorPane);
    }
    public static CMessageWindowStack getInstance(){
        if (null==window)
        {window=new CMessageWindowStack();}
        return window;
    }
/**
 * The core
 * @param sMessage
 * @param sType
 */
    private void updateMessages(final String sMessage, final String sType)

    {
        SwingUtilities.invokeLater( new Runnable() {
             public void run() {

        String sMessageHTML=""; 
        String sTypeText="";
        if (!sMessage.equals("\r\n")){ 
            sTypeText = sType+": ";
        }

        sMessageHTML = sMessage.replaceAll("(\r\n|\n)", "<br/>");
        if (!sMessageHTML.equals("<br/>")) 
        {
            sMessageHTML =   "<SPAN CLASS="+sType+">"+ sTypeText+sMessageHTML + "</SPAN>";
        }

        msgBuffer=msgBuffer.concat( sMessageHTML);
        editorPane.setText(msgBuffer);
        if ((editorPane.getDocument()).getLength()>1){
            editorPane.setCaretPosition((editorPane.getDocument()).getLength()-1);
        }  
             }
          });
    }

    public void setContainerFrame(JFrame jFrame){
        frameContainer = jFrame;
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(frameContainer.getContentPane());
        frameContainer.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(scrollPane)
                );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE))
                );
    }

    public void setVisible(boolean bVisible){
        editorPane.setVisible(bVisible);
        scrollPane.setVisible(bVisible);
    }

    public void printMsg(String sMessage){
        String sType = MESSAGE;
        updateMessages(sMessage,sType);
    }

    public void printlnMsg(String sMessage){
        sMessage=sMessage.concat("\r\n");
        printMsg(sMessage);
    }


    public static void main(String args[]){
        CMessageWindowStack m_LogMgr;
        JFrame frame = new JFrame();
        m_LogMgr=CMessageWindowStack.getInstance();
        m_LogMgr.setContainerFrame(frame);
        frame.setVisible(true);
        frame.setSize(500, 500);


        for(int i=0;i<10000;++i){
            try {
                Thread.sleep(0);//becomes unresponsive if sleep(0)
            } catch (Exception e) {
            }
            m_LogMgr.printlnMsg(i+"-----------------------");
        }

    }


}

Upvotes: 1

Views: 1000

Answers (3)

Klemen
Klemen

Reputation: 189

You can try using Thread and giving it highest priority. Also, you can add -XmX2020 option to java.exe.

Other solution may be if you use JTextArea instead of JEdiorPane. It seems like all you do with HTML is styling fonts. You can do this using java.awt.Font property on JTextArea.

Upvotes: 0

Randy
Randy

Reputation: 16673

well - as a hack you could try something like this:

    for(int i=0;i<10000;++i){
        try {
            if( i%100 == 0 ) {
                Thread.sleep(10);//becomes unresponsive if sleep(0)
            }
        } catch (Exception e) {
        }
        m_LogMgr.printlnMsg(i+"-----------------------");
    }

Upvotes: 0

camickr
camickr

Reputation: 324207

You would probably need to use a SwingWorker. Start by initially loading the JEditorPane with your basic HTML. Then you would need to publish each line of data. When the data gets published you would then need to insert the text into the editor pane.

The basic idea is you can't just create an entire HTML string at once.

I've never been able to really understand how to use a JEditorPane with dynamically changing HTML. Maybe you can just use a JTextPane. It supports different fonts and colors and is much easier to update dynamically.

Upvotes: 3

Related Questions