• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

insert image with swing

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, worked well
 
reply
    Bookmark Topic Watch Topic
  • New Topic