Add images to JButton in Eclipse

1.7k views Asked by At

I am a new programmer and have just started using Eclipse. Previously, I used JGRASP and when I wanted to add an image to a button all I did was to create an Image folder within the class's folder and write:

setIcon(newImageIcon(this.getClass().getResource("/Images/name.png")));

I tried the same thing with Eclipse but I get a main:NullPointerException error.
I already read another post and added a source folder to my project named Images but nothing changed. The following is the code segment I have an issue with:

resetButton.setIcon(newImageIcon(this.getClass().getResource("/Images/helpIcon.png")));

2

There are 2 answers

0
Alex On

You are on right way. You had added resource folder. This is the additional root for your resources. Here, in this folder (as root) you have to create /Image folder and then put files into it:

enter image description here

1
jpd On

You could also place your resources in the root "ttt" folder [Although this is not as professional a solution as the one Alex suggested] and refer to them by name, without any package details.