views:

184

answers:

3

I'm using NetBeans IDE 6.8 (Mac Version). which tool of their GUI builder will assist me in doing this?

What I want is is to show the user an image while my application is loading for couple of seconds before I show him the application. How can I do that? initializing

+8  A: 

If you have Java 6 installed, checkout the Splash-Screen tutorial.

Bart Kiers
+5  A: 

actually, you can do that by using the -splash flag in the java program... for example, you want to show the image splash.jpg when you run main.class,

so what you will do is,

java -splash:pathoftheimage/splash.jpg main

ultrajohn
Java 6 required.
Thorbjørn Ravn Andersen
+2  A: 

As you're running on a MAC you probably won't have access to Java 6 and so will have to build the splashscreen yourself. You should run code similar to the following early in your initialisation cycle (i.e. so that the splashscreen dialog is displayed for the maximum amount of time).

JDialog dlg = new JDialog();
// Remove dialog decorations to make it look like a splashscreen.
dlg.setUndecorated(true);
dlg.setModal(true);
dlg.setLayout(new BorderLayout());
// Load image.
ImageIcon img = new ImageIcon(getClass().getResource("/foo/bar/splash.png");
// Add image to center of dialog.
dlg.add(img, BorderLayout.CENTER);
dlg.setLocationRelativeTo(null);
dlg.setVisible(true);

// ... Perform application initialisation here.

// Initialisation complete so hide dialog.
dlg.setVisible(false);
dlg = null;
Adamski
Mac OS X does have Java 6 available since 10.5.
Joachim Sauer
What library should I import for the following class:BorderLayout();ImageIcon();
MAK
You'll need to import java.awt.* and javax.swing.* although if you're using a good IDE it should perform auto-imports. If not, you could use the JDK online docs to determine the packages to import: http://java.sun.com/javase/6/docs/api/
Adamski
@Joachim: I did not know that. @MAK: Given Joachim's response you should definitely try the "-splash" route first as it involves no code changes.
Adamski
well...i got exceptio error >> javax.swing.ImageIcon.<init>(ImageIcon.java:138)
MAK
@MAK: It's likely that your image isn't available (i.e. the image file isn't saved in the correct place). Remember, the resource is based on the app's classpath. If you're running your app from a jar or a "classes" directory created by your IDE then check the image file is actually in the jar or relevant directory. Without more information on the error that's the best I can suggest I'm afraid.
Adamski