Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find path to Image in Netbeans Plaform Maven Plugin

I try write plugin to NetBeans IDE. But I have problem with finding correct path to some image:

Here is my project tree structure:

enter image description here

And testing code like this dosent show icon wePagesBadge.gif

        ImageIcon image = new ImageIcon("webPagesBadge.gif");
        JOptionPane.showMessageDialog(null, "Some Messsage","Example",JOptionPane.INFORMATION_MESSAGE,image);

of course when i put this image into root folder where i have on my local computer installed netbeans the icon appear in JOptionPane. This path is

 C:\Program Files\NetBeans 8.0

Other example is when i not using Maven Project Type to develop plugin but Netbeans Module Project Type:

enter image description here

this time the image appear on the JOptionPane. I spent a lot time to find answer and I don't have idea how to fix it.

like image 709
Michał Ziembiński Avatar asked Mar 18 '23 15:03

Michał Ziembiński


2 Answers

It is a bad practice to put resources under the source package

create a folder my the name of resources under src/main/resources and then try to read it using

className.class.getClassLoader().getResource("yourImageFile");

So this should do the work

ImageIcon imageIcon = new ImageIcon(PDFGenerator.class
                                                .getClassLoader()
                                                .getResource("yourIcon.gif"));
like image 155
SparkOn Avatar answered Apr 06 '23 02:04

SparkOn


i found answer how to fix it. Insted of using code like:

    ImageIcon image = new ImageIcon("webPagesBadge.gif");
    JOptionPane.showMessageDialog(null, "Some Messsage","Example",JOptionPane.INFORMATION_MESSAGE,image);

i use

     ImageIcon image = new ImageIcon(ImageUtilities.loadImage("webPagesBadge.gif"));
     JOptionPane.showMessageDialog(null, "Some Messsage","Example",JOptionPane.INFORMATION_MESSAGE,image);

and i put image into:

enter image description here

Method which i use to load image

    ImageUtilities.loadImage("webPagesBadge.gif")

is from org.openide.util.ImageUtilities

like image 31
Michał Ziembiński Avatar answered Apr 06 '23 03:04

Michał Ziembiński