1,105,395 Community Members

retrieve image using java.net.URL

Member Avatar
learntosucceed
Newbie Poster
7 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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);
   }
Member Avatar
ejosiah
Junior Poster
196 posts since Feb 2008
Reputation Points: 4 [?]
Q&As Helped to Solve: 23 [?]
Skill Endorsements: 3 [?]
 
0
 

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

Member Avatar
learntosucceed
Newbie Poster
7 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

from the java site: http://java.sun.com/developer/techDo...TBG_Media.html

Isn't that what I need to put?

.../toolbarButtonGraphics/media/FastForward24.gif

Member Avatar
ejosiah
Junior Poster
196 posts since Feb 2008
Reputation Points: 4 [?]
Q&As Helped to Solve: 23 [?]
Skill Endorsements: 3 [?]
 
0
 

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

Member Avatar
learntosucceed
Newbie Poster
7 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
ejosiah
Junior Poster
196 posts since Feb 2008
Reputation Points: 4 [?]
Q&As Helped to Solve: 23 [?]
Skill Endorsements: 3 [?]
 
1
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article