🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #7920

Re: How do you load a graphic without using filename or url from within a project

#7920
From: Joi Ellis [TeamB]
Date: Tue, 28 Nov 2000 00:00
65 lines
3093 bytes
NOTE: I've canceled the original post due to the binary attachment.  
Please post binaries to the borland.public.attachments group and then refer
to them here.

I've quoted the text below and responded as well, so...

Philip Batey <philip_batey@hotmail.com> wrote:
> As you can see I have my images in a seperate dir to all my other code. I
> need to put these images into buttons on a tool bar but using a url of full
> path wont work when I deploy to a jar.

You need to use getClass.getResource() or getResourceAsStream()
to load the image.  Your existing code is using a simple file-open
operation and this is dependant upon your JVM/API/Application's handling
of "current working directory."  This handling varies by JVM version, platform,
etc, and can't be relied upon.  getResource() looks in the classpath and
is much safer.

> I wrote the class below in the same dir as the images to see if this would
> fix my problem. Now all I am getting is a single pixel in the middle of the
> button. Am I on the right track or have I lost the the plot all together

The swing load image methods launch a thread to perform the actual
image load.  You're seeing a small box because your main thread has attempted
to display the jpeg file before it has been fully loaded.

Here is a URL to a page in Sun's Swing tutorial that should lead you to
a few ways of handling it.  One way is to simply launch the image loads
earlier and use them later, which gives the load thread time to finish.
Another is to launch the load inside an invoke and wait (I think.)  There's
a MediaTracker class which handles monitoring the load process as well.

http://java.sun.com/docs/books/tutorial/uiswing/misc/example-swing/IconDemoApplet.java

http://java.sun.com/products//jdk/1.2/docs/api/java/awt/MediaTracker.html


> Please see a ttached jpg

> -------------------------
> package pagserver.images;

> import javax.swing.ImageIcon;

> public class AppImages
> {
>   public final String EXIT_IMAGE = "exitapp.gif";
>   public final String LOGIN_IMAGE = "UnLock.gif";
>   public final String LOGOUT_IMAGE = "Lock.gif";
>   public final String HELP_IMAGE = "help.gif";
>   public final String PAG_LOGO = "PA_LOGO.gif";

>   public ImageIcon getAppImage(String imageName)
>   {
>     ImageIcon i = new ImageIcon(imageName);
>     return i;
>   }
> }
> ------------------


-- 
Joi Ellis (TeamB) 
No direct email, please.
For best results, post on news server newsgroups.borland.com!
Technical Details
Message-ID: <900kn5$ga913@bornews.inprise.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <3a23cbba_1@dnews>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,1f5a07d7b07852a6,start
X-Google-Attributes: gid111d45,public
From: "Joi Ellis [TeamB]" <joi@aravox.com>
Subject: Re: How do you load a graphic without using filename or url from within a project
Date: 2000/11/28
Message-ID: <900kn5$ga913@bornews.inprise.com>
X-Deja-AN: 698723551
References: <3a23cbba_1@dnews>
Organization: Another Netscape Collabra Server User
User-Agent: tin/1.4.2-20000205 ("Possession") (UNIX) (Linux/2.2.14-5.0 (i686))
Newsgroups: borland.public.jbuilder.java.swing