I have been trying to make my program load the images while using web start with no success.

The following code is inside an extended JButton class. Image is a string containing the address of the desired image ("0.jpg", for example), I know that this would only work if the image is in the root.

public void displayIcon ()
    {
        setIcon(new ImageIcon(image));
    }

I tried using urls but they keep on being null.

Any ideas xD?

Thanks for the respond kramerd, but I have met a problem:

//Obtain the current classloader
ClassLoader classLoader = this.getClass().getClassLoader();
//Load the company logo image
Image companyLogo = classLoader.getResource("images/companyLogo.gif");

What getResource(String) returns is an URL not an Image, I tried using the URL but it did not work.

Is images/companyLogo.gif in a jar file?

I put my images inside the jar file (according to the instructions I got I just needed to put the images inside build/classes, I am using netbeans and then build and clean). The problem is that using those statements my program will not compile because Image companyLogo is expecting an image but is receiving an URL.

Edited 5 Years Ago by Kuroshi: n/a

Try this:

ClassLoader classLoader = this.getClass().getClassLoader();
java.net.URL imageURL   = classLoader.getResource("images/companyLogo.gif");
ImageIcon companyLogo = new ImageIcon(imageURL);

(Adapted from code found here)

hmmm,

are you tested

if(companyLogo == null){
System.err.println("My IconImage must be in same package as invoked class")
}

Try this:

ClassLoader classLoader = this.getClass().getClassLoader();
java.net.URL imageURL   = classLoader.getResource("images/companyLogo.gif");
ImageIcon companyLogo = new ImageIcon(imageURL);

(Adapted from code found here)

Thanks, it worked xD.

This question has already been answered. Start a new discussion instead.