user4920910
user4920910

Reputation:

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.

Upvotes: 1

Views: 99

Answers (1)

user4920910
user4920910

Reputation:

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

Upvotes: 2

Related Questions