• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing a icon to JLabel constructor

 
Ciaran Mooney
Ranch Hand
Posts: 74
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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

 
Campbell Ritchie
Sheriff
Pie
Posts: 49812
69
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49812
69
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok cheers
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic