public class Example {
    private static void createAndShowGUI() {

         JFrame frame = new JFrame("Window");

        JLabel emptyLabel = new JLabel("");
        emptyLabel.setPreferredSize(new Dimension(175, 100));
        frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);


    public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {

In The above code, what is the technical name for

javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {

in the main method.

Recommended Answers

All 7 Replies

build Swing GUI in Event Dispatch Thread,

Swing GUI must be done in EDT

delaying Swing GUI in EDT

more about technical ... in Concurency in Swing

Your new Runnable is an example of an anonymous inner class.

Thanks a lot, I guess the answer to my question was incredibly simple... I already knew what an anonymous class was, but what is the benefit to declaring a runnable as an anonymous class, can a class not extend runnable?

it seems to me like the Runnable is invoked because the EDT (which I have tried unsuccessfully to figure out on several occasions) can use Runnables in place of or along with a thread... yes? in that case, is the command 'invokeLater' being sent to the EDT? and if so, is javax.swing.SwingUtilities a static reference to the EDT?

1. Anonymous inner class is slightly shorter code, and keeps it all in one place, but you can have an ordinary named class that extends Runnable if you prefer.

2. The EDT is a Thread, which is managed by Swing. Swing maintains a queue of "things to do" (screen repaints, events to post etc) and runs them one at a time on the EDT. The "things" on that queue are Runnables. invokeLater is an ordinary method in the javax.swing.SwingUtilities class that simply places a reference to your Runnable on the queue. When your runnable gets to the front of the queue Swing calls your run method on the EDT thread.

Im so sorry to use up your time... I am very interested in this stuff...

I can see from your explanation that my understanding of this process was wrong... I thought the EDT ran the threads, but it IS a thread. But wait, how does the EDT differ from an ordinary thread? Is it because an EDT has an overriding 'run()' method?

'javax.swing.SwingUtilites' runs the invokeLater() method, but what is 'javax.swing.SwingUtilites' an instance of? I guess it is an instance of the Virtual Machine, (as it runs java threads) or is it the logical processing unit of the current machine? Or, is it just something else entirely?

The EDT is an ordinary thread. It's started somewhere in Swing's startup code. The only thing special about it is that there are lots of classes and methods within Swing that use it.

javax.swing.SwingUtilites is a perfectly ordinary class, like javax.swing.JLabel or java.lang.String. It has variables and methods like any other class, and one of those methods happens to be invokeLater. There's absolutely no magic here, none of these things is any different from classes and methods that you write yourself or threads that you start.

oh, ok, thanks a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.