JFrame example - how to display a JFrame

This morning when I saw some Java JFrame code on a mailing list, it made me think that I needed to put a simple JFrame example out here, something that would show how to properly construct and display a JFrame without getting into a discussion of anything else. Here are two examples that show the correct technique.

1) A simple JFrame example

To that end, here is the source code for a simple "JFrame example" demo class. This example shows how to construct a JFrame, and make sure it's properly displayed using the SwingUtilities invokeLater method:

import java.awt.*;
import javax.swing.*;

public class JFrameExample implements Runnable
{
    public static void main(String[] args)
    {
        JFrameExample example = new JFrameExample();
        // schedule this for the event dispatch thread (edt)
        SwingUtilities.invokeLater(example);
    }

    public void run()
    {
        JFrame frame = new JFrame("My JFrame Example");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(400, 200));
        frame.pack();
        frame.setVisible(true);
    }
}

Those are pretty much the minimal lines of code you need to construct and display a JFrame that (a) has the size you intended and (b) the user can close by selecting a quit/exit operation.

Note that the proper way to display a JFrame is by using the SwingUtilities invokeLater method to make sure this "job" is placed on the Event Dispatch Thread (EDT).

To make the code smaller I could reduce the main method to look like this:

// schedule this for the event dispatch thread (edt)
SwingUtilities.invokeLater(new JFrameExample());

but that can be a little hard for a new Java developer to deal with, so I left it as shown above.

2) A second JFrame example

Of course there are always different ways of doing things. If you'd rather not make your class Runnable, as shown in the first example, you can just create a Runnable instance on the fly, as shown in the main method of this second JFrame example:

import java.awt.*;
import javax.swing.*;

public class JFrameExample2
{
    public static void main(String[] args)
    {
        // schedule this for the event dispatch thread (edt)
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                displayJFrame();
            }
        });
    }

    static void displayJFrame()
    {
        JFrame frame = new JFrame("My JFrame Example");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(400, 200));
        frame.pack();
        frame.setVisible(true);
    }
}

This example does the same thing as the first JFrame example, but without making the class implement Runnable.

Laughing at myself a little bit here, it looks like I also showed a third way of doing this same thing in this other JFrame example.

Post new comment

The content of this field is kept private and will not be shown publicly.