Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to create a Java message dialog (swing/awt/other)?

I'm creating a Java application that will do some processing then needs to display a message to give the user feedback.

However, it appears to be incredibly slow - taking over two seconds to return.

I stripped the source down to the apparent culprit, and here is the code used:

package SwingPlay;

import javax.swing.JFrame;

public class Dialog
{

    public static void main( String[] args )
    {
        JFrame frame = new JFrame( "DialogDemo" );
    }

}

I'm executing this from the command line with:

java -classpath . SwingPlay.Dialog

As you can see - I'm doing nothing but create a JFrame, not even displaying it.

In case it is relevant, here is my java -version output:

java version "1.6.0_11"
Java(TM) SE Runtime Environment (build 1.6.0_11-b03)
Java HotSpot(TM) Client VM (build 11.0-b16, mixed mode, sharing)

And this is (currently) running against Win XP SP2.


So, first question: Why is it so slow?

More importantly, I just want a simple message (GUI, not cmdline) to be displayed without delay - can anyone provide some code to do this?


Update:

A bit of background might be helpful:
I am creating an application which will have many 'heads' (i.e. different user interfaces all using the same core classes to do the complex parts).
I currently have a pure command line head which works fine - responds straight away.
I will also have a standard application with a regular point & click GUI, and don't foresee problems with this bit.
What I am currently working on is a hybrid of these two - it will be launched from a Run box (or similar launcher), possibly with arguments, and only needs to respond with, effectively, a status message, that can be dismissed with a key press.

This latter one is where the question is focused.

Whilst I am not opposed to using my existing command line version with shell scripts (though didn't think it would be necessary!), the existing answers seem to suggest that things are not running as fast for me as they are for others - one example takes 1460ms for me, versus 70ms - a significant difference.

like image 976
Peter Boughton Avatar asked Feb 03 '09 20:02

Peter Boughton


People also ask

Which is faster AWT or Swing?

swing component provide much flexible user interface because it follow model view controller(mvc). awt is not mvc based. swing works faster. awt does not work faster.

What is AWT and Swing?

The full form of AWT is Abstract Window Toolkit. It has no full version. 2. It is an API used to develop window-based applications in Java. Swing is a graphical user interface (GUI) and a part of Oracle's Java Foundation Classes that are used to design different applications.


2 Answers

The reason for the delay it because Java is an interpreted language and it takes time to start a new JVM ( the interpreter )

Actually creating the frame takes less than a few ms ( about 70 ms in my machine ).

If this is going to be used within a Java app, you don't need to worry about it. It will be almost instantaneous ( you should use JDialog or JOptionPane for this )

If this is NOT going to be used inside a Java app, and 2 secs it too much ( and I think it is too much ) you should consider another tool for the job.

Here's how I measure the time in your code:

import javax.swing.JFrame;

public class Dialog {

    public static void main( String[] args ) {
        long start = System.currentTimeMillis();
        JFrame frame = new JFrame( "DialogDemo" );
        System.out.println( "Took: " + (  System.currentTimeMillis() - start   ) );
    }

}
like image 91
OscarRyz Avatar answered Sep 24 '22 08:09

OscarRyz


I would use a JOptionPane to show the message. Here's a simple example:

import javax.swing.*;

public class OptionDemo {
    public static void main(String[] args) throws Exception {
        JOptionPane.showMessageDialog(null, "Hello World");
    }
}

I'm afraid I can't explain the delay you're experiencing though. On my system, your code snippet runs in 500 milliseconds.

like image 44
Jason Day Avatar answered Sep 24 '22 08:09

Jason Day