• Post Reply Bookmark Topic Watch Topic
  • New Topic

insert image with swing  RSS feed

 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey everyone,

Im trying to insert an image using swing, but i get error cant find path am i declaring correctly doing this

ImageIcon convertIcon = createImageIcon("C:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures/Sunset.jpg",
"picture");

All help welcome and appreciated, thanks

Mark
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends on what the createImageIcon() method you are passing those values to does...
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
Here is the method

protected static ImageIcon createImageIcon(String path,
String description) {
java.net.URL imgURL = CelsiusConverter2.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}

all it is doing is displaying an image and a bit of text. The program runs fine, apart from saying error cant not find path for image, i assume my syntax for declaring the path was incorrect. Please advise

Mark
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The getResource method asks the class loader to find the resource on the classpath. If your
"My Documents" folder is not in your classpath the class loader comes up empty handed. You
can construct a new URL for the path. Just add the file protocol before the path. Some of
this is discussed in the section Loading Images Using getResource on the page How to Use Icons.

edit: typo
[ July 15, 2006: Message edited by: Craig Wood ]
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, worked well
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!