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 171171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:56:24+00:00 2026-05-11T12:56:24+00:00

I’ve got a couple thousand lines of code somewhere and I’ve noticed that my

  • 0

I’ve got a couple thousand lines of code somewhere and I’ve noticed that my JTextPane flickers when I update it too much.. I wrote a simplified version here:

import java.awt.*; import javax.swing.*;  public class Test {     static JFrame f;     static JTextPane a;     static final String NL = '\n';      public static void main(String... args)     {         EventQueue.invokeLater(new Runnable(){         public void run()         {         f = new JFrame();         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         f.setVisible(true);         f.setSize(400, 300);         f.setLocationRelativeTo(null);          a = new JTextPane();         f.add(new JScrollPane(a));          new Thread(new Runnable(){             public void run()             {                 int i = 0;                 StringBuffer b = new StringBuffer();                 while(true)                 {                     b.append(++i+NL);                     a.setText(b.toString());                     a.setCaretPosition(b.length());                     try{Thread.sleep(10);}catch(Exception e){}                 }             }         }).start();         }         });      } } 

This is for a terminal (cmd) style GUI component–

I think I’ve made all the optimizations I could here, including having \n as a final variable so it won’t be constructed hundreds of times. Still, the flickering is noticeable and unacceptable. After a few minutes, the component freezes completely. I must update the component very quickly, and the pane must be scrolled to the bottom when updated.

I’ve been thinking about making my own version of JTextPane from scratch, but I’d like to see if you guys have an easier solution.

  • 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. 2026-05-11T12:56:25+00:00Added an answer on May 11, 2026 at 12:56 pm

    Part of your error is that you are accessing a Swing component from outside the event thread! Yes, setText() is thread-safe, but Swing methods are not Thread-safe unless they are explicitly declared as such. Thus, setCaretPosition() is not Thread-safe and must be accessed from the event thread. This is almost certainly why your application eventually freezes.

    NOTE: JTextPane inherits its setText() method from JEditorPane and its setCaretPosition method from JTextComponent, which explains the links in the previous paragraph not going to the JTextPane JavaDoc page.

    To be Thread-safe, you really need to at least call setCaretPosition() from within the event thread, which you can do with code like this:

    SwingUtilities.invokeAndWait(new Runnable() {   public void run() {     a.setText(b.toString());     a.setCaretPosition(b.length());   } } 

    And since you have to call setCaretPosition() from within the event thread, you might as well also call setText() from the same place.

    It’s possible that you may not need to manually set the caret position. Check out the section ‘Caret Changes’ in the JavaDoc for JTextComponent.

    Finally, you may want to check out a series of two articles:

    • Faster JTextPane Text Insertion (Part I)
    • Faster JTextPane Text Insertion (Part II)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

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.