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

Java is the wrong tool for this. Setting up the JVM involves a lot of stuff happening in the background before the first line of Java code can be executed, and there's really no way to get around it.

If the JVM is the cause, why is it not slow when outputting to console, via System.out. Println('Text')? – Peter Boughton Feb 3 '09 at 20:57 2 @Peter: Because the JVM does not initialise the AWT sub-system if it is not needed.It's most likely that that is causing the delay in this case.

– Dan Dyer Feb 3 '09 at 21:44.

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 ) ); } }.

This specific bit of code could use a shell script, but I would still like to find out why I am getting such bad performance compared to you/others. – Peter Boughton Feb 3 '09 at 21:12 Takes about 400 to 500 ms on my machine (4-year old Pentium 3 M with 1.8 GHz). – Bombe Feb 3 '09 at 21:50 The 70 ms are elapse between the "long start ..." to the System.out... ( exactly as printed by the test ) From the console it takes about 1.5 secs.

But again, that's the JVM startup. From within a Java app you don't get that 1.5 extra. – OscarRyz Feb 3 '09 at 22:06.

Also it would be a lot faster to create an AWT Window (or maybe a Frame) instead of a JFrame because the latter has to pull in a gazillion of additional class files.

Whilst java.awt. Frame is faster, it's still over a second of delay. :( – Peter Boughton Feb 3 '09 at 20:36 Yes, unfortunately it isn’t really fast.

If you have everything in a single program, create the frame before performing the real action and then simply show it when you’re done. It won’t be faster overall but it will certainly look that way. – Bombe Feb 3 '09 at 21:16 The real action happens in a blink, so unfortunately I won't even gain that illusion... – Peter Boughton Feb 3 '09 at 21:31.

IF the box is coming from outside of your application, then you might want to use something else to generate a dialog. To make a native windows app that just shows a message box from a command line string would only take a few hours at most. Most of the common scripting languages should have ways to do it too.

Here's an example from some guy through javascript via command line: snee.com/bobdc.blog/2009/01/displaying-a....

Oh, and if you don’t really need to show the dialog from Java you could look into using KDialog (or it’s GNOME counterpart) or something similar.

The core of the application needs to be Java, but if using even a basic interface is too slow, then I can make do with using shell scripts to display the response from the program. – Peter Boughton Feb 3 '09 at 20:33 Yes, the first instantiation of something GUI-related in Java will always be slow(er), there’s no way to prevent that, short of going native (either via JNI or with Runtime.exec()). – Bombe Feb 3 '09 at 21:52.

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, " } } I'm afraid I can't explain the delay you're experiencing though. On my system, your code snippet runs in 500 milliseconds.

This seems roughly the same speed as the AWT example I tried (over a second, maybe around 1.5s), which is an improvement, but still annoyingly slow. If I could get it to 500ms that might be acceptable. – Peter Boughton Feb 3 '09 at 20:43.

What you're probably looking for is the new SplashScreen functionality in Java 6. Instead of having to wait for the JVM to load (there's always a cost to load any VM), this will load a screen beforehand.

I'm not looking to display a static image, but a dynamic piece of text. To put it another way, I want to redirect stdout to a GUI message box. If I do actually use System.out.

Println it comes back quickly enough, but obviously not in the correct format. – Peter Boughton Feb 3 '09 at 20:25.

You could use the JOptionDialog JOptionPane. ShowMessageDialog(parent frame, message, title, JOptionPane. MESSAGE_TYPE).

If there's a bottleneck deep inside the standard library, that's a good way to find it.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions