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?

According to this document:

All the files for your application must be stored in a set of JAR files, including resources such as images and sound files.

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?

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.