PoisonedYouth
PoisonedYouth

Reputation: 546

Java Agent Use Image Resource

I have got an Java Agent and I'm trying to use an ImageResource in the "JTreeCellRendererOpen" class from the "res" folder to show as image icon.

 ImageIcon icon = new ImageIcon(this.getClass().getResource("/res/ntf.gif"));
    super.setIcon(icon);

I try the different examples in the following NullPointerException when trying to use image resource but I always get an NullPointerException.

Does somebody know how to access the given resource?

enter image description here

Upvotes: 1

Views: 696

Answers (1)

Knut Herrmann
Knut Herrmann

Reputation: 30960

Use getResourceAsStream() instead of getResource():

import javax.swing.ImageIcon;
import javax.imageio.ImageIO;
...
        InputStream stream = this.getClass().getResourceAsStream("/ntf.gif");
        ImageIcon icon= new ImageIcon(ImageIO.read(stream));

Put the resource files in Java Agent into folder "Res" with button "Import / Resource":

enter image description here

You'll see the files in Package Explorer like this then:

enter image description here

Upvotes: 3

Related Questions