Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8644393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:16:58+00:00 2026-06-12T12:16:58+00:00

I am trying to use JEditorPane to display some HTML text. For some reason

  • 0

I am trying to use JEditorPane to display some HTML text. For some reason I have to use setText() method. However that makes the JEditorPane flickering. The reason is that every time after updating the editorPane.setText(msgBuffer); I have to bring the cursor to the end of document editorPane.setCaretPosition((editorPane.getDocument()).getLength()-1) asI want the most recent text line appearing at the bottom of the document.
I have bright you guys the entire code for you to see it for yourself. I have seen many recommendations as to use document.insertString, but for that matter I have to use individual attributes which is not of my interest.
Is there any way to make this code run flicker free?

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

public class CMessageWindow {

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

    private CMessageWindow()
    {
        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; }");
        styleSheet.addRule("."+ERROR+" {font: 10px monaco; color: #ff2222; background-color : #cccccc; }");

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

    {
        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 void printErr(String sMessage){
        String sType = ERROR;
        updateMessages(sMessage,sType);
    }

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

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

        for(int i=0;i<20;++i){
            m_LogMgr.printlnErr("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
        }

        for(int i=0;i<150;++i){
            try {
                Thread.sleep(20);
            } catch (Exception e) {
            }
            m_LogMgr.printlnMsg("-----------------------");
        }

    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:16:59+00:00Added an answer on June 12, 2026 at 12:16 pm

    You should not modify UI components outside the EDT.

    If you add a call inside, say, your updateMessages so that the update happens on the EDT then the flicker goes away.

    For example:

    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);
                }
            }
        });
    }
    

    Note that you shouldn’t perform long running operations on the EDT because otherwise you’ll “lock” your UI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying use eco for client-side templating. I have multiple .eco templates that I'd
I have a regex that I'm trying use to validate against strings. Trying to
I have a 3rd party DLL that I am trying to use in a
I am trying use Thread but i have some problem (I am beginner at
I'm trying to set tooltips on a JEditorPane . The method which I use
Hi I'm trying use a mixin to define some method using define_method. I would
I am trying use gem tire to search in my application. I have tables
Hi I'm trying use a datepicker on a field I have. I'm trying to
I am trying use javascript regular expressions to do some matching and I found
I have been trying use the edit_post_link() function to contain an image. All of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.