desmatron
desmatron

Reputation: 61

Unable to get image icon in runnable jar

I wrote a method in order to get icon for my swing:

public Icon getIcon(String iconName) {

    Icon icon = null;

    if(iconName.equals("NEXT")){
        icon = new ImageIcon( getClass().getResource("resources/img/next.png" ) );
    }

    return icon;
}

but

icon = new ImageIcon( getClass().getResource("resources/img/next.png" ) );

goes in null pointer

I created a source folder "resources" and a folder "img" inside it with "next.png" icon

Where's the problem?

Thanks

Upvotes: 2

Views: 4719

Answers (3)

Martin Sansone - MiOEE
Martin Sansone - MiOEE

Reputation: 4399

For those in need of help that have come across this page in Google - I wrote an answer in another StackOverflow question giving the best way to handle images in JAVA apps so that you can easily access the images for all image method types in Java:

This IS the best way to handle all images and icons in a JAR App.

Once you've zipped up all of your images and icons into its own JAR file - Configure your build path by adding the images JAR file into your libraries tab so that its now included in your classpath.

Then simply use the following 3x lines of code at the start of your constuctor to access any image you need for anything including a SystemTray image which doesn't accept the simple ImageIcon's as its main icon (weird I know). The 3x lines are:

  URL iconUrl = this.getClass().getResource("/image-iconb.png");
  Toolkit tk = this.getToolkit();
  someimgicon = tk.getImage(iconUrl);

(someimgicon is just a constructor declared Image variable) Now you can set a window icon as simply as:

setIconImage(someimgicon);

and at the same time use the same variable when setting the System TrayIcon by declaring:

 trayIcon = new TrayIcon(someimgicon, "SystemTray Demo", popupMenu);

The above allows you to declare Images or ImageIcons easily and centrally without running the risk of not keeping image resources in the right place. It keeps it nice and tidy, with the JAR containing all your images automatically compiled at run time and distribution of your program.

As a bonus, once the JAR is registered in your classpath - you can keep adding any other images into the same JAR at any time without any fuss too - Everything just works and the added images are instantly available to your app.

Much better in my view.

Upvotes: 1

desmatron
desmatron

Reputation: 61

so, I found the right method:

public static ImageIcon getImageIcon(String iconName) {

    ImageIcon imageIcon = null;

    if(iconName.equals("DOWNLOAD")){
        imageIcon = new ImageIcon(ImagesLocation.class.getResource("/img/download.png"));
    }

    return imageIcon;
}

with a "resources" source folder at the same level of the project and with an img folder inside (package styled)

ImagesLocation is a generic class containing this method

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691635

For this to work, the resources folder should be in the same folder as the folder corresponding to the package of this.getClass(). To start from the root of the classpath, use getClass().getResource("/resources/img/next.png"). (with a leading /)

Upvotes: 3

Related Questions