Question

I am studying Java Swing and I have some questions related to this simple code tutorial that I am reading:

package com.andrea.execute;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/* An istance of a SimpleExample class is a JFrame object: a top level container */
public class SimpleExample extends JFrame {

    /* Create a new SimpleExample object (that is a JFrame object) and set some property */
    public SimpleExample() {
        setTitle("Simple example");
        setSize(300, 200);
        setLocationRelativeTo(null);                // Center the window on the screen. 
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                SimpleExample ex = new SimpleExample();
                ex.setVisible(true);
            }
        });
    }
}

The logic is pretty simple: I have a SimpleExample class that inherits from JFrame Swing class. So a SimpleExample will be a toplevel container.

This class contain also the main() method and now I have 2 doubts:

1) Why in the main() method is execute this code:

SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                SimpleExample ex = new SimpleExample();
                ex.setVisible(true);
            }
        });

It call the invokeLater() method on the SwingUtilities class and pass to it a new Runnable oject.

Reading the documentation I know that is a way to places the application on the Swing Event Queue. It is used to ensure that all UI updates are concurrency-safe.

The thing that I have some problem to understand is how it is implemented.

Inside the input parameter of invokeLater() method it pass this "stuff":

new Runnable() {
        @Override
        public void run() {
            SimpleExample ex = new SimpleExample();
            ex.setVisible(true);
        }
    });

What is it this stuff? What does it represent? and How does it work?

Was it helpful?

Solution

It is an anonymous class that implements Runnable interface.

You can use it without anonymous class like:

class SimpleExample extends JFrame {

    public SimpleExample() {
        setTitle("Simple example");
        setSize(300, 200);
        setLocationRelativeTo(null);                
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new App());
    }
}

class App implements Runnable {

    public void run() {
        SimpleExample ex = new SimpleExample();
        ex.setVisible(true);
    }
}

But anonymous class is more convenient.

OTHER TIPS

Runnable is an interface that contain the method run. The invokeLater need this implementation of runnable to invoke the passed run method from the swing thread.

See this link to have better information http://en.wikibooks.org/wiki/Java_Programming/Threads_and_Runnables

javax.swing.SwingUtilities#invokeLater(Runnable doRun), schedules a java.lang.Runnable for execution on the event thread (callable from any thread);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top