2

I have a problem adding ImageIcon to a JMenuItem. I added it just like it was written in the tutorial but it didn't work. I have checked on Google what's wrong and didn't find. This is the code:

File.add(new JMenuItem("New", new ImageIcon("images/new.gif")));

I thought the problem might be that the icon isn't in the same folder as the .class or .java files, so I have replaced it - and still not working. The menu shows just the text.. What should I do to make i work?

Filburt
  • 17,626
  • 12
  • 64
  • 115
Roni Copul
  • 183
  • 1
  • 1
  • 8
  • 3
    I would try to give a full file path in order to test if isn't really the file name. – Francisco Spaeth Jul 07 '12 at 18:06
  • 2
    and whats happends when you rename File(reserved word in Java) to fileMenu, btw test if images/new.gif isn't null – mKorbel Jul 07 '12 at 18:08
  • Francisco Spaeth - I don't think it will matter because the menu and all the other program is working, except for the icons.. mKorbel - I don't think it will help because I have used "File" many times before.. but I'll try – Roni Copul Jul 07 '12 at 18:18
  • What I'm trying to check if this is due the file location or file format... if it will work with the full path file the problem is with the file location if not it is due the file format. – Francisco Spaeth Jul 07 '12 at 19:00
  • 1
    @RoniCopul : Please have a look at this answer of mine, [HOW TO ADD IMAGES TO YOUR PROJECT](http://stackoverflow.com/a/9866659/1057230). What you can do for a quick fix is, simply paste your images folder next to your package (if any), if no package exists, simply place it next to .class files, and use the wonderful guidance, suggested by "JBNizet" – nIcE cOw Jul 08 '12 at 06:10

1 Answers1

4

You're passing a relative file name as argument. This file name is not relative to a class. It's relative to the JVM's current directory. And the current directory if the directory from which java is started. So if you launch the program from c:\foo, it will look for the file c:\foo\images\new.gif, even if the classes are in the jar file d:\Java\myApp.jar or in the directory e:\projects\myApp\classes.

Files used as ImageIcon are usually bundled in the jar file of the application, along with the class files, and loaded by the class loader. If you store the file under the package com.foo.bar, you should thus use

new ImageIcon(MyClass.class.getResource("/com/foo/bar/new.gif"));
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • First of all - thanks a lot for the answer! But I still didn't understand it clearly - where do I need to link the icon to (or from? sorry my English isn't that good :) ). In simple words - what should I write in the ( ) ? – Roni Copul Jul 07 '12 at 22:13
  • I don't understand. Which parentheses? – JB Nizet Jul 07 '12 at 22:16
  • Oh nice to know how the () is called... And I ment the ImageIcon() parentheses – Roni Copul Jul 07 '12 at 22:20
  • +1, for the complete insight. I learned some mistakes I am committing while answering at some other threads regarding the same issue, thingies related to JVM's current directory and not relative to class :-) – nIcE cOw Jul 08 '12 at 06:17