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 4 of 4

Thread: What is the difference?

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Location
    Hong Kong
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What is the difference?

    Hi there,
    Just new to Java.

    I am reading a book in which there are 2 ways to start the program.

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable()
        {
    	public void run()
    	{
    		JFrame frame = new TestFrame();
    		...
                    ...
    	}
    	});
    }

    The other way is without the EventQueue.invokeLater():
    public static void main(String[] args) {
         JFrame frame = new TestFrame();
         ...
         ...
    }

    Tried both of them, and both of them worked.

    So why should I put the EventQueue.invokeLater() in the codes? (I read the API document, but just don't really understand...)

    Thx.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,442
    My Mood
    Mellow
    Thanks
    86
    Thanked 475 Times in 466 Posts

    Default Re: What is the difference?

    The first starts the Swing application on the Event Dispatch Thread which is the correct way. The latter starts the Swing ap on the main thread which is the incorrect way. Yes, the incorrect way will work and may never present problems, but when problems do occur, they will be difficult to identify and correct, wasting a lot of unnecessary time.

    Edit: I use SwingUtilities rather than EventQueue. They may be the same, but I'll have to look into it.

  3. #3
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: What is the difference?


  4. #4
    Junior Member
    Join Date
    Oct 2013
    Location
    Hong Kong
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is the difference?

    Thx. You guys are so helpful.

Similar Threads

  1. What is the difference between these two?
    By Java Trainer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 7th, 2013, 02:19 AM
  2. Difference ++j and J++
    By rayan2004 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 4th, 2012, 12:42 AM
  3. What is the difference?
    By soul_samir in forum Java Theory & Questions
    Replies: 1
    Last Post: February 11th, 2012, 02:08 PM
  4. Difference between jboss 4.x,5.x and 6.x
    By vairam in forum Member Introductions
    Replies: 1
    Last Post: May 3rd, 2011, 09:49 AM
  5. Need to know this difference
    By arvind in forum Java Theory & Questions
    Replies: 2
    Last Post: January 19th, 2010, 05:24 AM