Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Why should Swing GUI code be placed on the EDT?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    25
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Why should Swing GUI code be placed on the EDT?

    According to what I read, “when programming in Swing, your GUI creation code should be placed on the Event Dispatch Thread (EDT). This will prevent potential race conditions that could lead to deadlock.” (See below for code.)

    Why is this? How could making a GUI lead to deadlock?

        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Why should Swing GUI code be placed on the EDT?

    Also posted here and here.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    25
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Why should Swing GUI code be placed on the EDT?

    As I wrote in the other topic: are these forums somehow connected and, if so, is there a preferred place?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why should Swing GUI code be placed on the EDT?

    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Why should Swing GUI code be placed on the EDT?

    Believe me, things get weird if you dont put all your Swing code on the EDT. Certain components might disappear, or perhaps they get rendered at the wrong location, sometimes the entire Swing application will become just a white color plane.
    Why? Because swing was designed to be used on the EDT and if you dont invoke all Swing methods on the EDT you do it wrong. Just believe me, bad things WILL happen sooner or later.

  6. #6
    Junior Member
    Join Date
    Jun 2014
    Posts
    25
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Why should Swing GUI code be placed on the EDT?

    Do I also need to use the above code (explicitly put the Swing code on the EDT) if my entire program runs in one threat, i.e. my program never explicitly creates any threats?
    Last edited by evert67; October 3rd, 2014 at 09:14 AM.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Why should Swing GUI code be placed on the EDT?

    There are several good articles on the 'net on this topic. Search 'java swing edt', 'java swing thread safe', 'java swing multi-threading', etc.

  8. #8
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Why should Swing GUI code be placed on the EDT?

    If you run your entire program on the EDT then every swing action will obviously run on the EDT as well and you are good.
    However, any heavy computations should NOT be run on the EDT or the application migth become unresponsive. If you need to access any databases, files, have any networking code, or just in general do any heavy work, put it into a worker thread.

Similar Threads

  1. Run code to scan file system after rendering Swing GUI window
    By kyleabaker in forum AWT / Java Swing
    Replies: 2
    Last Post: September 30th, 2014, 11:21 AM
  2. [SOLVED] SWING:- GUI And Image Problems.
    By BlueEyesWhiteDragon in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 10th, 2014, 07:37 AM
  3. Basic GUI (eclipse swing)
    By klskl in forum Object Oriented Programming
    Replies: 20
    Last Post: November 17th, 2013, 04:50 PM
  4. Simple swing GUI code....Plz help
    By Arati2512 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 13th, 2012, 11:13 AM
  5. Help with swing GUI
    By mwr76 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 5th, 2011, 03:54 AM