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);
    }
}
Matthew Frost
  • 578
  • 5
  • 13
  • Your code runs both the way you wrote it (from the entry thread) and if you run it on the EDT like Hovercraft suggests. It's just that your way can cause problems in the future. – user1803551 Jan 31 '16 at 17:12
  • So did you try the code with -XstartOnFirstThread, and it opened a 450 x 450 window with no issues? @user1803551 – Matthew Frost Jan 31 '16 at 17:17
  • No, I started it regularly the way it is written and on the EDT. To be clear, you are not attempting to run it on the EDT with or without your flag. – user1803551 Jan 31 '16 at 17:20
  • My question is addressing the fact that it wont work when you run it using -XstartOnFirstThread. The solution to this question would therefore be getting it running the same way it does without using -XstartOnFirstThread, using -XstartOnFirstThread. @user1803551 – Matthew Frost Jan 31 '16 at 17:23
  • Yes, yet you don't show or say that you are trying to run it on the EDT. – user1803551 Jan 31 '16 at 17:25
  • As far as I'm aware all Swing event handling code runs on a special thread known as the event dispatch thread. However, the behaviour that the example program exhibits when you run it using XstartOnFirstThread indicates to me that there is something going wrong with the EDT. Do you have a solution to this? @ user1803551 – Matthew Frost Jan 31 '16 at 17:29
  • "*all Swing event handling code runs on [the EDT]*" Only if you make it so. "*the behaviour that the example program exhibits when you run it using XstartOnFirstThread indicates to me that there is something going wrong with the EDT.*" Not yet, there is no mention of the EDT anywhere in your code. Maybe if you do set it to run from the EDT it won't work, and *then* you have a valid question. – user1803551 Jan 31 '16 at 17:32
  • I took "_Swing event handling code runs on a special thread known as the event dispatch thread._" directly from [here](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html). So are you saying that Oracle have worded this incorrectly? – Matthew Frost Jan 31 '16 at 17:38
  • Might be relevant: http://stackoverflow.com/questions/28149634/what-does-the-xstartonfirstthread-vm-argument-do-mean – user1803551 Jan 31 '16 at 18:33
  • Looking at the answer I linked, the one linked in it and other sources, the flag is not relevant to Swing applications. Why do you need to do this? – user1803551 Jan 31 '16 at 18:47
  • To avoid over complicating the question. This part is a small part of a much larger system, that has to initially run with -XstartOnFirstThread. – Matthew Frost Jan 31 '16 at 19:51

1 Answers1

1

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
});
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373