Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image Icons Not Found

Status
Not open for further replies.

Nutthick

MIS
Jan 14, 2004
126
GB
I'm trying to get icons to appear on my command buttons, but no matter where I put them on the HDD, they don't seem to appear. I though placing them in the same directories as the source must work but even nothing then. My code is as follows:

Code:
import javax.swing.*;

public class IconFrame extends JFrame
{
    JButton load, save, subscribe, unsubscribe;
    
    public IconFrame()
    {
        super("Icon Frame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        
        // Create Icons
        ImageIcon loadIcon = new ImageIcon("load.gif");
        ImageIcon saveIcon = new ImageIcon("save.gif");
        ImageIcon subscribeIcon = new ImageIcon("subscribe.gif");
        ImageIcon unsubscribeIcon = new ImageIcon("unsubscribe.gif");
        
        // Create Buttons
        load = new JButton("Load", loadIcon);
        save = new JButton("Save", saveIcon);
        subscribe = new JButton("Subscribe", subscribeIcon);
        unsubscribe = new JButton("Unsubscribe", unsubscribeIcon);
        
        // Add Button to Panel
        panel.add(load);
        panel.add(save);
        panel.add(subscribe);
        panel.add(unsubscribe);

        
        // Add the panel to the frame
        add(panel);
        pack();
        setVisible(true);
    }
    
    public static void main(String[] arguments)
    {
        IconFrame ike = new IconFrame();
    }
}

Can anyone see what I'm doing wrong?

Thanks
 
You have to remember that you have not put a full path to the images, so if you have your images in "/usr/local/images" and run your app from "/home/somewhere" it will look for the images in "/home/somewhere" - because that is the local directory relevant to where you start the application - it does not look for the source directory.

Try adding the full path to the images.

--------------------------------------------------
Free Database Connection Pooling Software
 
Another way, which allows you to easily put your images into a jar file along with your code, is to have the image in the same directory as the compiled .class file. An image would then be loaded with something like...
Code:
ImageIcon loadIcon;
URL imageURL = IconFrame.class.getResource("load.gif");
if ( imageURL != null ){
    loadIcon = new ImageIcon(imageURL);
}

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
The full path works, but what I would prefer is to dump all my graphics into their own directory, and then have my compiler look at it. I'm using Netbeans 4.1 and it doesn't seem to want to do it, or I can't find the option setting.

timw - I've been trying to get a jar file ,full of graphics, from the sun site to work, but again when I compile nothing is found. I can't find anything on the Netbeans site about it either. I've tried importing the jar file as a library, and also placing it into the 'class' folder, but still nothing.

Any ideas, I appreciate not everyone uses Netbeans. Thanks for the help so far.
 
I don't use NetBeans.

Can you take me through the steps you tried so far with respect to getting a working Jar file with code and graphics?

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Using GIF files

Placing them in the same directory as the .java files and referencing them as "load.gif" (Doesn't Work)

Placing them in the same directory as the .java files and referencing them as with a full path (Works).

n.b. Anything in the .java folder gets copied to where the .class folders are placed.

Placing a class path to the file (Doesn't work)

Using a JAR file

I'm not sure how to call a graphic from a JAR file, I've tried a few syntaxs, but nothing.

Tried JAR in the .java directory and then calling "load.gif" (Doesn't work)

Tried JAR in a directory with other JARs that Netbeans is using and then calling "load.gif" (Doesn't work)

Tried importing JAR as a library and then calling "load.gif" (Doesn't work)

I've been messing with options in Netbeans, but nothing is working. I assume my syntax is wrong, but then the syntax for calling from a library must be standard to the language and not Netbeans.

Really confusing.
 
The code I posted will work regardless of whether the code/graphics are packaged in a jar or not. As long as the image file is in the same directory as the .class file which loads it, all should be fine.

When you've created your jar file, have you looked in it (using Winzip or equivalent) to ensure everything is there and in the correct place?

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
The graphics JAR looks fine, loads of GIFs.

I tried your code, but I keep getting the following error

Code:
symbol  : class URL
location: class IconFrame
        URL imageURL = IconFrame.class.getResource("About16.gif");
1 error
BUILD FAILED (total time: 0 seconds)
 
I've solved it, sort of, by modifying your code as follows

Code:
ImageIcon loadIcon = new ImageIcon(IconFrame.class.getResource("/graphics/About16.gif"));

Still think it's a bit wierd. Also if anyone knows how to get the Netbeans library working, then I would appreciate knowing.

timw - Thanks for all your help, you got me there in the end.
 
Using "/graphics/About16.gif" in a getResource() call indicates that your gif is in a 'graphics' sub-context within the jar file. If that is where it's always been, and the .class is in the root context (since I didn't see a package declaration in your code above), it's not surprising my code didn't work.

You have it working, so that's fine.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
I just put all the graphics into a 'graphics' folder, once I'd cracked the syntax you helped me with. Just my attempt to get some order introduced into the system.

Thanks again
 
Order is always a good thing [smile].

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top