Situation: I have an application running, and when the user clicks a button on the main menu, a new dialog opens.
Problem: This dialog takes awhile to load as it is retreiving information from the database etc ... hence I would like to launch a splash screen in one thread and the other dialog in a second thread.
Following is the Splash Screen class:
public class SplashScreen extends JWindow {
public SplashScreen(int d) {
duration = d;
this.launchSplash();
}
public void launchSplash() {
JPanel content = (JPanel) getContentPane();
content.setBackground(Color.white);
// Set the window's bounds, centering the window
int width = 350;
int height = 255;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width - width) / 2;
int y = (screen.height - height) / 2;
setBounds(x, y, width, height);
// Build the splash screen
JLabel label = new JLabel();
label.setIcon(new ImageIcon(getClass().getResource("loading.gif")) );
label.setHorizontalAlignment(JLabel.CENTER);
content.add(label, BorderLayout.CENTER);
// Display it
this.setVisible(true);
// Wait a little while, maybe while loading resources
try {
Thread.sleep(duration);
} catch (Exception e) {
}
this.setVisible(false);
// System.exit(0);
}
// public static void main(String[] args) {
//
// // Throw a nice little title page up on the screen first
// SplashScreen splash = new SplashScreen(10000);
//
// // Normally, we'd call splash.launchSplash() and get on
// // with the program. But, since this is only a test...
//
// System.exit(0);
//
// }
When I run this as a standalone, it works fine (i.e. uncomment the 'main' function). But when I try to call it from another function, it doesn't launch.
Eg: I am calling it when a button has been clicked. Code is as follows:
public void actionPerformed(ActionEvent e) {
SplashScreen sd = new SplashScreen(10000);
// Calling the dialog (that takes a long time to load) here
:
:
}
Would like to know whether
[1] is it possible to launch a splash screen on the click of a button? As usually a splash screen is done at the beginning of an application before anything is loaded. In my case an application is already running, during which a splash screen needs to be launched.
[2] Would using the new splashscreen class of Java 6.0 support this?
[3] If not what is the best way to approach this problem
Appreciate any input.
Thanks,
Edited by: mnr on Feb 20, 2008 9:47 AM