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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:48:43+00:00 2026-06-15T00:48:43+00:00

I have encountered a very strange problem when trying to insert multiple Strings into

  • 0

I have encountered a very strange problem when trying to insert multiple Strings into a JTextPane that has a TitledBorder (this is important, the problem seems to only occur with TitledBorder specifically, other Borders or no Border at all work fine) from a Thread. The (for this problem) significant part of my test code looks like this:

JTextPane myTextPane = new JTextPane();
myTextPane.setBorder(new TitledBorder("Some title"));
StyledDocument doc = myTextPane.getStyledDocument();
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setForeground(sas, Color.BLACK);

private void insertTwoStrings()
{
    new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            docTest.insertString(docTest.getLength(), "first string ", sas);
            docTest.insertString(docTest.getLength(), "second string\n", sas);
        }
    }).start();
}

Now to the problem: the insertTwoStrings() method works fine sometimes, but other times it fails very badly in a way that locks up the entire application (i have to kill the process to shut the application down). So i opened the program in the debugger and replicated the problem there, when the problematic Thread got locked up i suspended it and took a closer look at the program counter position:

Current position of program counter

It seems like this synchronized(this) is the cause of my problem. Is this actually a bug or am i making some kind of mistake?

In case someone wants to replicate this, for the problem to occur you have to meet all of the following three criteria:

  1. inserting text into a JTextPane (i.e. into its StyledDocument) from a Thread
  2. invoking StyledDocument.insertString(…) multiple times within the thread, not just once
  3. the JTextPane must have a TitledBorder

Executing the Thread will still work sometimes, but every now and then it will fail and lock up the entire program.

  • 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-15T00:48:43+00:00Added an answer on June 15, 2026 at 12:48 am

    As others already point out Swing is not threadsafe unless the specific method is documented to be threadsafe. Swing provides several Utility methods and classes that can pass messages to the event dispatch thread – SwingUtilities contains invokeLater and invokeAndWait, the SwingWorker threads can send “finished” work to a callback.

    // run will be executed on the EDT
    SwingUtilities.invokeLater(new Runnable()
    {
        @Override
        public void run()
        {
            docTest.insertString(docTest.getLength(), "first string ", sas);
            docTest.insertString(docTest.getLength(), "second string\n", sas);
        }
    });
    

    The above code will execute run on swings event dispatch thread, so take care to keep run() short (read fast) or your User interface will slow to a crawl.

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

Sidebar

Related Questions

We have encountered a very strange class not found problem in our web app
I have encountered a very strange problem in using jQuery load() . I used
I have encountered a very strange problem. The jquery slideshow in the below webpage
I have just encountered very strange problem - my GWT app hosted on Google
I have encountered a very strange problem and I was wondering, if anyone could
I have encountered a very weird problem with fancybox modal that whenever my website
I have encountered a most annoying problem that occurs on the PWD variable when
I have encountered a problem of placing data into the Table View. Here is
i have encountered a very strange issue: i use json.dump to write a file
I have encountered a very strange issue. Whenever I use the .NET membership provider

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.