I tried to load a picture in my JDialog. So, I tried with JLabel like that:

ImageIcon icon = new ImageIcon("/sau/se/editor/images/HighBtomStPic.png");
System.out.println("Icon exists: "+(icon != null));
JLabel lblNewLabel = new JLabel();
lblNewLabel.setIcon(icon);

The print gives me true and I don't see the picture I have inserted, so what am I doing wrong?

有帮助吗?

解决方案 2

Since I am dealing with source context not a file system one,

The answer should be: To replace:

ImageIcon icon = new ImageIcon("/sau/se/editor/images/HighBtomStPic.png");

With:

ImageIcon icon = new ImageIcon(GraphEditor.class.getResource("/sau/se/editor/images/HighBtomStPic‌​.png"));

其他提示

The constructor always returns an instance if it doesn't throw an exception. This is why the null check seems ok.

Instead, you should check to see that: icon.getImageLoadStatus() == MediaTracker.COMPLETE

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top