0

I have the following code in my program:

JLabel status = new JLabel();  
status.setBorder(BorderFactory.createLineBorder(Color.BLACK));  
status.setBounds(400, 30, 380, 200);
status.setIcon(new ImageIcon("C:........./logo2.png")); 
status.setLocation(50, 750);
add(status); 

But now I want to remove the path from setIcon(new ImageIcon("path")) to an image that I have inside a folder in my project.

I tried to do this:

status.setIcon(new ImageIcon("src/folder/logo2.png")); 

But it didn't display anything.

I find this topic and tried this too:

ImageIcon(this.getClass().getResource("src/files/logo2.png"));

But it gives me a NullPointer.

This is the structure of my project:

enter image description here

How can I display this image correctly?

Community
  • 1
  • 1
Rikkin
  • 509
  • 1
  • 5
  • 20
  • Please, show folders structure of your project. – Alexey Semenyuk Oct 02 '14 at 19:43
  • ok, I just edited my question – Rikkin Oct 02 '14 at 19:49
  • 2
    Did you try `ImageIcon(this.getClass().getResource("/files/logo2.png"));`? – Alexey Semenyuk Oct 02 '14 at 19:53
  • First, don't include the reference to "src", ever, in your code, "src" will not exist once the app is built. Second, AFAIK, resources can't be stored within the "src" directory and should be stored within some kind of resources directory at the same level as the "src" directory and included within the class/build path of eclipse for the given project. – MadProgrammer Oct 02 '14 at 20:26
  • @MadProgrammer, [Second statement not true](http://stackoverflow.com/a/25636097/2587435) :-) – Paul Samsotha Oct 03 '14 at 01:40
  • @peeskillet Really, then why does it never work for people? – MadProgrammer Oct 03 '14 at 01:42
  • @MadProgrammer I don't know. Everything in the src is included, just like a regular package. That's the default behavior – Paul Samsotha Oct 03 '14 at 01:44
  • @peeskillet Does it work from within the IDE as well? – MadProgrammer Oct 03 '14 at 01:46
  • @MadProgrammer Yea, everything in the src gets copied to the `bin`, and that's where all the classes are compiled to when you run. Just like netbeans temporarily copies everything and compiles to the `build` – Paul Samsotha Oct 03 '14 at 01:48
  • @Rikkin: Hopefully this thread, related to, how to [add images to Java Project](http://stackoverflow.com/a/9866659/1057230), might be of some help on the topic :-) – nIcE cOw Oct 03 '14 at 03:11
  • BTW - Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Oct 04 '14 at 02:10

1 Answers1

0

Look you have to create a new Folder under the Project folder, call it resources or any thing you want. After that you can refer to your resources as getClass().getResource("resources/thefile.png").toFile() Hope that helps

BilalDja
  • 1,072
  • 1
  • 9
  • 17