Swing Splashscreen

Hi,

I am a Java/Swing newbie, trying to convert from C++. I am writing a small app to interface with a PostgreSQL DB. I would like to include a splashscreen, and have used the code on sun's website (see below). I call this from the main().

The splashscreen loads however it is simply a grey box. I have no idea what is wrong with it? Anybody have any suggestions, much appreciated. Thanks!

////////////////////////////////////////// SPLASH SCREEN////////////////////////////////////////////

public class SplashScreen extends JWindow {

private int duration;

public SplashScreen(int d) {

duration = d;

}

// A simple little method to show a title screen in the center

// of the screen for the amount of time given in the constructor

public void showSplash() {

JPanel content = (JPanel)getContentPane();

content.setBackground(Color.BLUE);

// Set the window's bounds, centering the window

int width = 450;

int height =315;

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(new ImageIcon("/home/ehellaby/lehman.jpg"));

JLabel copyrt = new JLabel

("Lehman Brothers. Developed by Edward Hellaby", JLabel.CENTER);

copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));

content.add(label, BorderLayout.CENTER);

content.add(copyrt, BorderLayout.SOUTH);

Color oraRed = new Color(156, 20, 20, 255);

content.setBorder(BorderFactory.createLineBorder(oraRed, 10));

// Display it

setVisible(true);

// Wait a little while, maybe while loading resources

try { Thread.sleep(duration); } catch (Exception e) {}

setVisible(false);

}

public void showSplashAndExit() {

showSplash();

System.exit(0);

}

}

[1993 byte] By [ehellabya] at [2007-10-2 16:14:25]
# 1

When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting ]Formatting tips[/url] on the message entry page. It makes it much easier to read.

Does the image "/home/ehellaby/lehman.jpg" exist, and is it readable?

mlka at 2007-7-13 17:02:59 > top of Java-index,Desktop,I18N...
# 2

Sorry about the formatting.

Yes the image does exist, and is readable. I have even tried commenting the image code out to double check that problem. Im just at a loss as to what i can do next.

The screen shot of the splash screen that is displayed can be seen here....

http://esnips.com/doc/0d45668f-e933-45a3-a653-e3ebfccde3b3/snapshot2.jpg

Thanks

ehellabya at 2007-7-13 17:02:59 > top of Java-index,Desktop,I18N...
# 3

Wild guess: Is the paint called before Thread.sleep? Try forcing a repaint before calling Thread.sleep and see what happens.

Otherwise in the great tradition of copy paste programming ;) try: http://www.devdaily.com/java/edu/SplashScreen/

or

http://pipasoft.com/free-java-classes/

MichaelW13a at 2007-7-13 17:02:59 > top of Java-index,Desktop,I18N...
# 4

OK, i think i got it.

If i take out the setVisible(false) the splash screen eventually loads up as it should once the rest of my app gets painted to the screen.

I have to find where to put the call to splash.showSplashScreen().

If you see below i have tried after setting my MDIForm as visible. But this never doesnt work.

Sorry if this seems daft, as i say i am a bit of a newbie to the graphics stuff!

Cheers

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

try{

new MDIScreen().setVisible(true);

} catch(Exception e){}

SplashScreen splash = new SplashScreen(1000);

splash.showSplash();

}

});

}

ehellabya at 2007-7-13 17:02:59 > top of Java-index,Desktop,I18N...