GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Passing a icon to JLabel constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Passing a icon to JLabel constructor" Watch "Passing a icon to JLabel constructor" New topic
Author

Passing a icon to JLabel constructor

Ciaran Mooney
Ranch Hand

Joined: Jan 21, 2013
Posts: 72

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


I love deadlines, I love the whooshing sound they make as they fly by...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39062
    
  23
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

Joined: Jan 21, 2013
Posts: 72

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

Joined: Jan 21, 2013
Posts: 72

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
Sheriff

Joined: Oct 13, 2005
Posts: 39062
    
  23
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

Joined: Jan 21, 2013
Posts: 72

Ok cheers
 
GeeCON Prague 2014
 
subject: Passing a icon to JLabel constructor