Matthew Frost
Matthew Frost

Reputation: 608

Running a JFrame in thread 0

When running a program that makes a JFrame (Swing), why is it that if it runs on thread 0 it does not show the window? Running on thread 0 can be done by (OS X):

java -XstartOnFirstThread Driver

Example

public class Driver
{
    public static void main (String args[])
    {   
        SwingUtilities.invokeLater(() -> { 
            WindowClass button = new WindowClass(450, 450);
        });
    }
}

public class WindowClass extends JFrame
{
    public WindowClass(int width, int height)
    {
        setTitle("Demo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(width, height);
        setVisible(true);
    }
}

Upvotes: 0

Views: 138

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285415

You need to show code, but you run code on the EDT by queuing it on the EDT using SwingUtilities:

SwingUtilities.invokeLater(() -> {
    // start your GUI here
});

Upvotes: 1

Related Questions