Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #18864 > unrolled thread

How can I add an image to a label, so that it gets into a jar file

Started byclusardi2k@aol.com
First post2012-09-20 09:48 -0700
Last post2012-09-20 21:51 -0700
Articles 3 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  How can I add an image to a label, so that it gets into a jar file clusardi2k@aol.com - 2012-09-20 09:48 -0700
    Re: How can I add an image to a label, so that it gets into a jar file clusardi2k@aol.com - 2012-09-20 09:49 -0700
    Re: How can I add an image to a label, so that it gets into a jar file Lew <lewbloch@gmail.com> - 2012-09-20 21:51 -0700

#18864 — How can I add an image to a label, so that it gets into a jar file

Fromclusardi2k@aol.com
Date2012-09-20 09:48 -0700
SubjectHow can I add an image to a label, so that it gets into a jar file
Message-ID<b8383a9e-bdbc-4b30-b181-93205d388313@googlegroups.com>
How can I add an image to a label, so that it gets into the associated jar at build time.

   URL url = Test.class.getResource("/baby_face.gif"); 
                
   Icon addimage = new ImageIcon(url); 
   jlabel1.setIcon(addimage);

compile:
run:
Exception in thread "main" java.lang.NullPointerException
	at test_1.BTest.<init>(Test.java:234)
	at bltest_1.Test.main(Test_main.java:10)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

If I look at "addimage" in the debugger, I see:

addimage
 filename null
 imageObserver null
 accessibleContext null
 description "file:/C:/..."
 width   76
 height  40
...

Thank you,

[toc] | [next] | [standalone]


#18865

Fromclusardi2k@aol.com
Date2012-09-20 09:49 -0700
Message-ID<b19ccb15-1a87-466f-9fd4-2b5bf57ae2f6@googlegroups.com>
In reply to#18864
Reference:

http://www.coderanch.com/t/555675/java/java/Images-jar

[toc] | [prev] | [next] | [standalone]


#18872

FromLew <lewbloch@gmail.com>
Date2012-09-20 21:51 -0700
Message-ID<66540df0-1719-4628-84d5-cff19ad761c4@googlegroups.com>
In reply to#18864
 (unknown) wrote:
> How can I add an image to a label, so that it gets into the associated jar at build time.
> 
>    URL url = Test.class.getResource("/baby_face.gif"); 

Have you read the Javadocs?
http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource(java.lang.String)

It searches the classpath, so place the resource in the classpath, i.e., somewhere in the / tree of 
the JAR. For the string you show, that'd be the root of the JAR internal tree.

Study up on classpaths and JARs.

Also, check out http://sscce.org/

-- 
Lew

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web