Reputation: 35
for some reason i cannot load certain images with my java program. just above this snippet of code i have another image referral that works fine.
for(int x = 1; x<=7; x++){
if(additionals[x] != 0){
rightPanel.add(new JLabel(new ImageIcon("IMAGES/image"+x+".GIF")));
count++;
}
}
images are saved in a folder named IMAGES and are called image1.gif, image2.gif etc if you need the rest of my code just ask
Upvotes: 0
Views: 139
Reputation: 285415
Are you sure that this:
if (additionals[0] != 0){
rightPanel.add(new JLabel(new ImageIcon("IMAGES/ram"+additionals[0]+"gb.gif")));
count++;
}
for(int x = 1; x<=7; x++){
if (additionals[x] != 0){
rightPanel.add(new JLabel(new ImageIcon("IMAGES/image"+x+".gif")));
count++;
}
}
shouldn't really be this?
if (additionals[0] != 0){
rightPanel.add(new JLabel(new ImageIcon("IMAGES/ram"+additionals[0]+"gb.gif")));
count++;
}
for(int x = 1; x<=7; x++){
if (additionals[x] != 0){
rightPanel.add(new JLabel(new ImageIcon("IMAGES/image"+ additionals[x]+".gif")));
count++;
}
}
?
It would make your code appear more symmetric. Otherwise, do printlns before the offending line with the String that you want to use to make the ImageIcon to be sure that it's correct.
For example:
for(int x = 1; x<=7; x++){
if (additionals[x] != 0){
String imagePath = "IMAGES/image"+x+".gif";
System.out.println("imagePath = " + imagePath);
rightPanel.add(new JLabel(new ImageIcon(imagePath)));
count++;
}
}
And then compare the Strings that are output with the file names and paths. Even better is to create a new File and output its full path before trying to use it to create a new ImageIcon.
caveat: code has not been tested.
Upvotes: 1
Reputation: 34478
GIF != gif
otherwise(additionals[x] != 0)
?rightPanel
big enough for all images?Upvotes: 2