I'm trying to use these icon images for my JButtons, but I keep getting "Resource not found: "
http://java.sun.com/developer/techDocs/hi/repository/TBG_Media.html

static ImageIcon createMediaIcon(String imageName) 
   {
      String imgLocation = "/toolbarButtonGraphics/media/" + imageName + ".gif";
      java.net.URL imageURL = Test.class.getResource(imgLocation);
      if (imageURL == null) 
      {
         System.err.println("Resource not found: " + imgLocation);
            return null;
      } 
      else
         return new ImageIcon(imageURL);
   }

Recommended Answers

where is this path /toolbarButtonGraphics/media/ relative to your Test.class path?

Jump to Post

create a URL object passing it the url path you want and then pass your URL object to the ImageIcon's constructor. take a look at the javadoc for java.net.URL

Jump to Post

All 5 Replies

where is this path /toolbarButtonGraphics/media/ relative to your Test.class path?

The path has to be in your application directory; if you are trying to download the image off the internet then you have to get it use the URL class itself

can you give me an example of how to use this URL class? I think that sounds more efficient if I want my friend to run the program too on his computer.

create a URL object passing it the url path you want and then pass your URL object to the ImageIcon's constructor. take a look at the javadoc for java.net.URL

Be a part of the DaniWeb community

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