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.

Edited 5 Years Ago by FALL3N: n/a

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?

Edited 5 Years Ago by FALL3N: n/a

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.

This article has been dead for over six months. Start a new discussion instead.