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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:22:22+00:00 2026-05-26T09:22:22+00:00

I learned about how swing isn’t thread-safe. Delving deeper, I discovered that every modification

  • 0

I learned about how swing isn’t thread-safe. Delving deeper, I discovered that every modification to a swing component must be done on the Event Dispatch Thread in order to prevent various issues associated with multithreading. However, the information seemed to completely stop there. There doesn’t seem to be a good tutorial that explains how to do this anywhere accessible on the internet.

Patching together information from code posted in relation to other issues, it seemed that I would have to put an untidy block of code around every single swing modification in my program (like this example from my own code):

try {
        SwingUtilities.invokeAndWait(new Runnable() {

            public void run() {
                setTitle("Frame title");
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setVisible(true);

                setSize(800, 480);
                setLocationRelativeTo(null);
                setIconImage(Toolkit.getDefaultToolkit().createImage(ClassLoader.getSystemResource("Frame icon.png")));
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }

Basically, is this right? Do I have to put that code (or the equivalent with invokeLater) around every modification to a Swing component in my code?

Also, why doesn’t Swing do this automatically?

  • 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-05-26T09:22:22+00:00Added an answer on May 26, 2026 at 9:22 am

    The trick is that when swing calls you it will ALWAYS be in the EDT, so you don’t have to worry about it.

    However if you are in a timer or an action triggered by some other external event, your main thread or any other thread you’ve created then yes, you have to use invokeLater or invokeAndWait.

    In other words, yes swing does do “it” automatically. Needing to use invokeXx is so rare that if swing were to do it internally it would waste too much time.

    Many java programmers never figure this out and it can cause some pretty nasty hard-to-find problems with drawing your GUI. I do wish swing threw an exception when you called it not using the EDT–Java would have a better reputation when it came to professional GUIs if it did because there would be less crap out there.

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

Sidebar

Related Questions

I just learned about ngrep , a cool program that lets you easily sniff
So far I have learned about generating thread dump and heap dump using jstack
I just learned about Google App Engine, and I must say it is fantastic.
One of the first things I've learned about Java EE development is that I
I'm taking some AI classes and have learned about some basic algorithms that I
From my littleexperience in Flex, I learned about Bindable variables, so that the content
I learned about location tag that can be used to disable the inheritance for
I learned about the Comparable interface, for which a class must implement compareTo method.
I learned about Microsoft BizSpark the other day and started the sign up process.
I just learned about how the Java Collections Framework implements data structures in linked

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.