1

I really have a problem adding images to my java project, and before you ask, yes i have searched already and tried everything, but i just can't get it working.

Here is my problem:

At the moment i am using this code to get the images:

        ImageIcon goldIcon = new ImageIcon("res/Gold_coin.png");
        ImageIcon silverIcon = new ImageIcon("res/Silver_icon.png");
        ImageIcon copperIcon = new ImageIcon("res/Copper_icon.png");

My project structure is as following:

I have one project folder with two sub folders.

Both sub folders are specified as source folders, one is the "src" folder and the other one i named "res". In the "src" folder i have one package with all classes in it. In the "res" folder i have all the images saved.

Now the strange thing is, the "Gold_icon" DOES work, but both silver and copper do NOT. I am using eclipse luna and if someone could give me a step by step instruction how to add an image would be really nice.

Because all i find is always "add to resource" , "add it to resources folder" and honestly, i tried creatig a new folder, i copied it to the "src" folder, i tried every possible call, from ("res/Gold_coin.png") over ("/Gold_coin.png") to ("Gold_coin.png") and ("/res/Gold_coin.png")

I refreshed the project, the folders, the package, the classes, i restarted eclipse but nothing helps

I just don't get it..

Please help :(

If you need the information what i want to do with this images afterwards, i am adding them together into a JPanel with flowlayout which i write into a JTable cell with a cellrenderer, which is everything working with the gold icon, but not the other two. And it also does not work to remove the gold icon (because i thought maybe for whatever reason only the first icon works..) but then nothing is displayed

Folder structure and adding imageicons

Adding imageicons to JPanel

Output

BlackOutDev
  • 99
  • 1
  • 1
  • 12
  • 1
    Your images have `coin` in name but yu use `icon`. Change to `Silver_coin` and take a rest. – Volodymyr Levytskyi Apr 11 '15 at 18:06
  • Please have a look at this thread, regarding [how to add images to Resource folder in Java](http://stackoverflow.com/a/9866659/1057230). Hope it helps :-) – nIcE cOw Apr 11 '15 at 18:06
  • Oh my god my head just went totally red! Can't believe this was the error... I guess i focused to much on coding issues.. thanks so much now it works! – BlackOutDev Apr 11 '15 at 18:10

1 Answers1

4

You should use getResource to load images or whatever from resource folder.

For example:

String pathToImage = "res/Gold_coin.png";
ImageIcon myIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImage));

or with all project path: nameOfProject/res/Gold_coin.png.

rbrisuda
  • 975
  • 9
  • 20
  • I have tried both, and both do not work :( at this point i have a question, i tried also the full name of path, and my project name contains spaces, does this matter? – BlackOutDev Apr 11 '15 at 17:44
  • What mean that "but both silver and copper do NOT"? Is object null? – rbrisuda Apr 11 '15 at 17:49
  • I added some pictures to the main post to clarify stuff. With getResource i get a nullpointer exception, with just calling by path nothing will be added without error – BlackOutDev Apr 11 '15 at 17:58
  • With this way, you should copy "res" directory to "src" directory because all "src" directory (with code and other files) is compiled and copied to bin folder. Than, all resources are loaded by getResource from bin folder. – rbrisuda Apr 11 '15 at 18:19