public class Example {
    
    private static void createAndShowGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);

         JFrame frame = new JFrame("Window");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

        frame.pack();
        frame.setVisible(true);
    }

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

In The above code, what is the technical name for

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

in the main method.

Dani AI

Generated

In the example from that appears in main(), the precise technical description is: scheduling (or enqueueing) a Runnable to run on the Swing Event Dispatch Thread (EDT). Common short phrases you will see are "post a Runnable to the EDT", "dispatch a task to the EventQueue", or "schedule an asynchronous GUI task on the EDT." All mean the same thing: the Runnable is placed on Swing's event queue and will be executed later by the single thread that handles GUI events.

The object passed is an anonymous inner class implementing Runnable (as explained). Small wording correction: you implement the Runnable interface (not extend it). In modern Java you can write the same behavior more concisely with a lambda, for example:

SwingUtilities.invokeLater(() -> createAndShowGUI());

Behavioral notes and practical cautions: invokeLater is asynchronous—it returns immediately and the Runnable runs later on the EDT. If you need to wait until the task completes, use invokeAndWait (but never call invokeAndWait from the EDT itself, that would deadlock). SwingUtilities is a convenience class that delegates to the AWT EventQueue; it is not the EDT itself.

For robust Swing apps: always perform GUI construction and updates on the EDT, but keep long-running work off the EDT (use SwingWorker or background threads) to avoid freezing the UI. To test where code is running at runtime, use SwingUtilities.isEventDispatchThread() before doing GUI work. This summary ties together 's point about doing Swing work on the EDT and 's comments about the Runnable form, and clarifies the preferred terminology and a couple of common pitfalls.

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.