Frage

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?

War es hilfreich?

Lösung 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"));

Andere Tipps

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top