954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

What is the technical name for the call to javax.swing.SwingUtilities.invokeLater?

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.

FALL3N
Junior Poster in Training
84 posts since May 2010
Reputation Points: 10
Solved Threads: 2
 

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

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

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

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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?

FALL3N
Junior Poster in Training
84 posts since May 2010
Reputation Points: 10
Solved Threads: 2
 

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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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?

FALL3N
Junior Poster in Training
84 posts since May 2010
Reputation Points: 10
Solved Threads: 2
 

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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

oh, ok, thanks a lot!

FALL3N
Junior Poster in Training
84 posts since May 2010
Reputation Points: 10
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: