Issue displaying images
I am very new to Swing, have only been learning for a few days now. I am trying to just display an image. When I compile and run, the image is found, however only the text and not the icon itself is displayed. Does anyone know why this might happen?
import javax.swing.*;
import java.awt.*;
public class PusoySwing{
public static void main(String[]args){
JFrame frame = new JFrame("Holds Cards");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon icon = createImageIcon("images/2diamond.png", "two of diamonds");
JLabel holdImage = new JLabel("Image and Text", icon, JLabel.CENTER);
frame.getContentPane().add(holdImage);
frame.pack();
frame.setVisible(true);
}
[754 byte] By [
alphtaa] at [2007-11-27 4:37:45]

# 1
Your posted code:
a) isn't formatted, so its hard to read
b) has multiple compile errors, so I don't know how you are testing it.
Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html]How to Use Icons[/url] for a working example.
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.
# 2
// try to use
ImageIcon icon = new ImageIcon("images/2diamond.png", "two of diamonds")
// instead of
// ImageIcon icon = new ImageIcon("images/2diamond.png",
// "two of diamonds");
For me, this code is working properly.
import javax.swing.*;
import java.awt.*;
public class PusoySwing
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Holds Cards");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon icon = new ImageIcon("images/2diamond.png", "two of diamonds");
JLabel holdImage = new JLabel("Image and Text", icon, JLabel.CENTER);
frame.getContentPane().add(holdImage);
frame.pack();
frame.setVisible(true);
}
}
# 3
I read through that whole thing earlier. My compile errors were probably because I did not include the whole thing. Here is the whole code.
import javax.swing.*;
import java.awt.*;
public class PusoySwing{
public static void main(String[]args){
JFrame frame = new JFrame("Holds Cards");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon icon = createImageIcon("images/2diamond.png", "two of diamonds");
JLabel holdImage = new JLabel("Image and Text", icon, JLabel.CENTER);
frame.getContentPane().add(holdImage);
frame.pack();
frame.setVisible(true);
}
private static ImageIcon createImageIcon(String path, String description) {
java.net.URL imgURL = PusoySwing.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
}
# 4
Your code (with method createImageIcon) is working just fine on my computer.Which version of Java are you using, and what kind of IDE? I tried it with Notepad & command line compiler, and no display problem at all.
# 5
sztyopek , I tried that and the image still will not display for some reason. The image is in an images folder which is in the same folder as the .java program.Do I need to somehow configure my compiler to accept this image, or should it just display?
# 6
I am using JCreater 3.5 LE and Java 5
# 7
Try this:1. create a command line shortcut to the folder your .java file and image subfolder is placed to,2. open it, and make sure it has the same startup directory. If not, change it.3. start your application: java PusoySwing4. then tell me the result.
# 8
> I am using JCreater 3.5 LE and Java 5 Try placing the image in the same directory as the class file.If that doesn't work try testing the program from the command line. It sounds like you have an IDE that is not set up correctly as your posted code also works for
# 9
I did that, and got a Java window with the text "Image and Text". The image did not display
# 10
Placing the image in the same directory does not work either. I will try downloading a newer IDE
# 11
Hmm, the image does not display in JCreater LE 4.0 either
# 12
Try it with another image (press Alt-PrtScr on any window to copy it to the clipboard, then paste the image into mspaint.exe, and save it as a PNG to the java image subfolder).Try the same thing with a JPG image.
# 13
It worked with a black and white image. Ill experiment and try to make it work with others. Thakns for your help