The Wrench in the System The Wrench in the System - 5 months ago 7
Java Question

Eclipse Java application can't find resources

I have a splash screen for an an application that I am making. It is a

JWindow
with a
JPanel
and a
JLabel
I then have an
ImageIcon
on the
JLabel
. The
ImageIcon
is loaded from an
InputStream
using
this.getClass.getResourceAsStream("GenericApp.png");
. My code for the splash screen is below:

final JWindow window = new JWindow();
JPanel jp = new JPanel();
InputStream is = this.getClass().getResourceAsStream("GenericApp.png");
Image image = null;
try {
image = ImageIO.read(is);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JLabel l = new JLabel(new ImageIcon(image));
window.add(jp);
jp.add(l);
window.setBounds(500, 150, 300, 200);
window.setVisible(true);
try{
Thread.sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}
window.setVisible(false);


When I launch my project, I get a blank JWindow with the dimensions I set.

Answer

Here is how I solved it:

Apparently, the image takes a while to load. I was calling Thread.sleep(5000) before it finished loading the image and thus cutting off the display process. The moral of the story is that you almost never want to use Thread.sleep() USE TIMERS INSTEAD.

Regards,

Thomas