EventQueue.invokeLater vrs SwingUtilities.invokeLater
Asked Answered
P

3

6

Can someone highlight on the differences between these two and the instances both are required?!

I have an application which uses both intercheably, but want to know if one is better than the other. Obviously they both accept Runnable object, and so for me - I think I can use which one I like.

Why these two similar functions in different classes? I know one is in awt and the other Swing, but don't they do the same thing?

Pitching answered 26/4, 2012 at 12:34 Comment(1)
possible duplicate of SwingUtilities.invokeLater() vs EventQueue.invokeLater()Homemaker
S
14

SwingUtilities.invokeLater exists only because EventQueue.invokelater was introduced in 1.2, but Swing was available for 1.1. Swing in the JRE has always just called the EventQueue version. swingall.jar had some hack where it created a component, and executed pending operations on repaints.

invokeLater is about the EventQueue. I suggest using the method directly. SwingUtilities is just a dumping ground. I have seen a lot using SwingUtilities.invokeLater presumably in some kind of belief the Swing isn't dependent upon AWT.

Sonny answered 26/4, 2012 at 12:43 Comment(0)
G
5

Actually the code of SwingUtilities#invokeLater(Runnable doRun) is :

public static void invokeLater(Runnable doRun) {
    EventQueue.invokeLater(doRun);
}

So it's exactly the same thing!

Gravid answered 26/4, 2012 at 12:40 Comment(0)
A
4

As described in the javadoc, they are the same, e.g. a copy-paste from the invokeAndWait method javadoc

As of 1.3 this method is just a cover for java.awt.EventQueue.invokeAndWait()

So you can mix them and it does not matter which version you use.

Accumulate answered 26/4, 2012 at 12:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.