0

I tried to add an jpg image file into my Java Swing page, but when running, system always back error "Source not found" and stucking there. the source code are following

this.setLayout( new BorderLayout( ) );
URL url = getClass().getResource("logo");
ImageIcon imageicon = new ImageIcon( url );
JLabel label = new JLabel( imageicon );
this.add( label, BorderLayout.NORTH );

The file name is: "unStudent.java", and image file is "logo". I have put the both files in the same folder, why system can not find image file? what should I change?

Thanks in advance. Tony

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    Where is "logo" stored and also, is that the correct name? What IDE are you using? – MadProgrammer Jul 19 '14 at 00:52
  • I have change image file's path to whole path as below, this.setLayout( new BorderLayout( ) ); URL url = getClass().getResource("C:\\Documents and Settings\\evergreen\\workspace\\login\\src\\login\\logo"); ImageIcon imageicon = new ImageIcon( url ); JLabel label = new JLabel( imageicon ); this.add( label, BorderLayout.NORTH ); – user3837183 Jul 19 '14 at 00:55
  • but error is same, I use Eclipse – user3837183 Jul 19 '14 at 00:56
  • Two things, I might consider using `getClass().getResource("/login/logo")` instead and you might need to move the `logo` file out of the `src` directory to the "resources" directory within in the project directory as Eclipse doesn't like resources been stored in the `src` directory...generally... – MadProgrammer Jul 19 '14 at 01:14
  • Usually, where should this image file put, under /src and with *.java files? – user3837183 Jul 19 '14 at 01:25
  • Eclipse doesn't tend to like anything else other the *.java files to be stored in the source directory. Resources should be stored in a `resources` folder within the same directory as the `src` folder. You might need to tell Eclipse to include this directory as part of it's run/build process, not 100% sure as I don't use Eclipse, I just know the issue exists – MadProgrammer Jul 19 '14 at 01:32
  • @user3837183: Please have a look at how to [add images to Java project in Eclipse](http://stackoverflow.com/a/9278270/1057230) and how to [add images to Java Project](http://stackoverflow.com/a/9866659/1057230), for some detailed insight. Hopefully this be of some help in your endeavour :-) – nIcE cOw Jul 19 '14 at 04:58
  • Thank you MadProgrammer, I switch to higher version Eclipse and put logo file under /bin forder, then I can see the image. I believe old Eclipse has bug for handling image – user3837183 Jul 20 '14 at 02:02

2 Answers2

0

Try this method to create your ImageIcon. Make it public or private, it's up to your design.

private ImageIcon createIcon(String path)
{
    URL url = getClass().getResource(path);

    if(url == null)
    {
        System.err.println("Could not load the icon: " + path);
    }

    ImageIcon icon = new ImageIcon(url);

    return icon;
}

now make sure that path is the correct path to the file and with the file extension. I'm quite sure your mistake, or one of your mistakes, is the file extension missing. So if your logo file is a png image type and it is stored in c:\images your path must be c:\images\logo.png

I would recommend you to place your images inside your project folder.

PS; pay attention to \ escaping. I assume you know it.

dazito
  • 7,740
  • 15
  • 75
  • 117
  • This isn't adding anything to what the OP is already doing. You shouldn't be using getResource with absolute paths, that's the point of using getResource. – MadProgrammer Jul 19 '14 at 04:27
  • Thank you Sir. I switch to higher version Eclipse and put logo file under /bin folder, then I can see the image. I believe old Eclipse has bug for handling image – user3837183 Jul 20 '14 at 02:04
-1

Try this soluion:

URL url = getClass().getResource("logo.jpg");

if it does not works try to put the absolute path of your image as follows:

URL url = new URL("C://folder/logo.jpg");
Youcam39
  • 102
  • 5