• 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

Passing a icon to JLabel constructor

 
Ranch Hand
Posts: 74
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
I am having trouble creating a valid URL source for a Icon, not sure if its something to do with the Classpath and where the JVM is looking for the jpeg file in the Imageicon constructor.


Icon pic = new ImageIcon(getClass().getResource("Me Passport Photo.jpg"));

label2 = new JLabel("Label 2 with text and Icon obj.", pic, SwingConstants.LEFT);



The JVM throws an exception when trying to source the jpeg.

The jpeg is actually on my desktop, I am using a Mac OS Lion.

Ps cant upload java class and app files just yet as on a mobile.

Cheers
Ciaran

 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell us the details, otherwise we can’t help. Start by telling us where the image files you were looking for are. And what sort of error did you suffer? If it was an Exception, show us its stack trace and tell us what the line numbers are. Have you set a system classpath (that usually does a lot more harm than good)?
And please not only one thread per question, but also one question per thread. Get the image thing sorted out before you try anything on the mobile. You just get confused if you try too many things simultaneously.
 
Ciaran Mooney
Ranch Hand
Posts: 74
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The problem was i wasn't putting the image file in the
projects src folder (i had them sitting on my desktop).
It works fine now.

Cheers
C

 
Ciaran Mooney
Ranch Hand
Posts: 74
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Come to think of it, is it possible to specify
a URL in the Imageicon constructor so a image can be loaded from any directory
besides the package SRC files?

Also, if i have a large image is it posdible to set the dimensions so it fits in a JComponment ie a button or a label
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It probably is, unless you are using getClass getResource. Start with the Java Tutorials and try ctrl‑F‑image, and you get about half a dozen hits.
 
Ciaran Mooney
Ranch Hand
Posts: 74
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok cheers
 
This tiny ad will self destruct in five seconds.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic