2

I'm trying to simply load a photo to jpanel but it is not working.

import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.io.File;
import javax.swing.*;

public class ImageTest3 extends JFrame {

    public ImageTest3() {

        // TODO Auto-generated constructor stub
        JPanel panel = new JPanel(new FlowLayout());
        Icon icon = new ImageIcon(
                File.separator + "resources" 
                    + File.separator + "images"
                    + File.separator + "topbar.jpg");
        JLabel label1 = new JLabel();
        panel.add(label1);
        add(panel);
        label1.setIcon(icon);

        setSize(2000,700);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new ImageTest3();
    }
}

Sorry for the crappy form... The file is saved in my \src\resources\images\topbar.jpg

What am I doing wrong ? I also had it as ImageIcon icon = new ImageIcon ....

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • I hope this [link](http://stackoverflow.com/a/9866659/1057230) might be able to sort things out for you :-) – nIcE cOw Jul 04 '13 at 16:16
  • The info part of [embedded-resource](http://stackoverflow.com/tags/embedded-resource/info), can also point you in a better way. – nIcE cOw Jul 04 '13 at 16:36

1 Answers1

0

You should load the image as a Resource, as follows

String pathToImageSortBy = "resources"+File.separator+ "images"+ File.separator+"topbar.jpg";
Icon icon = newImageIcon(
                         getClass().getClassLoader().getResource(pathToImageSortBy));
Cristian Meneses
  • 4,013
  • 17
  • 32
  • Thanks for the response but I got a NullPointerException. The File is in the proper spot and the file name and such look correct. Any ideas? –  Jul 04 '13 at 16:19
  • Can you try putting a File.separator before "resources" ? – Cristian Meneses Jul 04 '13 at 16:24
  • I guess the use of `getClassLoader()` is really not the right way, as suggested by the [Java Docs](http://docs.oracle.com/javase/7/docs/technotes/guides/lang/resources.html) - __" If not found, the methods return null. A resource may be found in a different entry in the CLASSPATH than the location where the class file was loaded."__ – nIcE cOw Jul 04 '13 at 16:24
  • You can also use a single slash instead of File.separator, since it is a resource – Cristian Meneses Jul 04 '13 at 16:33
  • @christian That was my first response and still a null pointer –  Jul 04 '13 at 16:40
  • Are the class and the image on the same jar ? If not, are both if them in the classpath ? I've made a test using same jar, and works – Cristian Meneses Jul 04 '13 at 16:49
  • Yeah so i cant post a photo, Im not on my actual account. So I tried readding the photos. So .java files are ImageTest\src\defaultpackage\ImageTest3\ and the photos are ImageTest\resources\images\topbar.jpg. ImageTest being the project name –  Jul 04 '13 at 17:01
  • On the readded i accidentally didnt put it in the \src\. That was probably the error. I dont know why it wasnt working before though –  Jul 04 '13 at 17:11
  • `File.separator` is the wrong char for `getResource()` - it should be `/` on all platforms. – Andrew Thompson Jul 07 '13 at 01:49