2

I started writing a simple Hello world app with JFrame, and I noticed that the first window that I create (or the first reference to Swing libraries) takes 15 seconds to show. I also tested it with the compiled application launched from console to see if there is a difference, and there is none.

Here's my code:

public static void main(String args[]) {

    /* Create and display the form */
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            long start = System.currentTimeMillis();
            new Test().setVisible(true);
            System.out.println("FORM 1: " + (System.currentTimeMillis() - start));

            start = System.currentTimeMillis();
            new Test2().setVisible(true);
            System.out.println("FORM 2: " + (System.currentTimeMillis() - start));
        }
    });
}

And here's output:

FORM 1: 15362
FORM 2: 203

I've read about disabling D3D for Java2D but it's no use for me as I work on linux (openSUSE). I also tried switching from Oracle's JDK 1.8 to openJDK 1.8 but it still doesn't make any difference.

What can I do to speed that thing up?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
FilipK
  • 125
  • 13
  • That is only a very tiny insignificant snippet of your code. – Gimby Nov 10 '17 at 12:21
  • The rest is just code of two empty forms generated by Netbeans. How would that help you? – FilipK Nov 10 '17 at 12:54
  • It won't except that I can then see that it won't help rather than you leaving that up as guess work. Hence you have no information to provide that allows people to answer you. Consider trying the more modern JavaFX if you want to do a GUI application; Swing is old, cranky and not further developed anymore. You may just be running into a problem with your particular hardware that Swing or its OpenGL driver layer does not like. – Gimby Nov 10 '17 at 13:16
  • Thanks for a tip. I'll check that out – FilipK Nov 10 '17 at 13:38
  • Just to be sure: you also tried a simple hello world console application and that did not have startup problems right? It'd be rather frustrating if it was a problem of the Java runtime installation itself and not specifically Swing. – Gimby Nov 10 '17 at 13:43
  • I've tried hello world as console application and it starts instantly. Also in Swing application, when I print something just before using Swing then it would print instantly too, hang for 15 seconds and display JFrame – FilipK Nov 10 '17 at 13:49
  • For a better benchmark, opens Test2() before Test() – Marcos Vasconcelos Nov 10 '17 at 17:05

2 Answers2

0

Try running something like this:

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        long start = System.currentTimeMillis();
        new JFrame().setVisible(true);
        System.out.println("FORM 1: " + (System.currentTimeMillis() - start));
    }
});

If it's still taking a long time, then you know it's definitely Swing - unfortunately though if you've tried the usual approaches, there's not a great deal you can do about it (aside from perhaps trying Java 7 and/or filing a bug report with details of your system.) This may be worth a read, but is heavily based around windows, so much of it won't apply.

If it's much faster running this code, then there's some other aspect of your code (or the IDE's auto-generated code) that's causing the issue.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
0

I wasn't aware of current Java "trends" so I didn't know about creating Java GUI apps with JavaFX as last time I tried creating GUI apps in Java was about 5 years ago and then I only knew about Swing.

As suggested by Gimby in comments I tried JavaFX and that solved my problem.

FilipK
  • 125
  • 13