Article View: borland.public.jbuilder.java.swing
Article #1323Re: How to use images from up directories?
From: Paul Furbacher
Date: Wed, 07 Jan 2004 15:57
Date: Wed, 07 Jan 2004 15:57
172 lines
5749 bytes
5749 bytes
Kevin Dean wrote:
> I ran into similar problems some time ago. The issue is that Java doesn't
> support ".." directories when locating resources.
Actually, I think it does. Here's some code that works.
Given the following directory
../projects
/thisproject
/src
/images
- logo.png
... a bunch of other icons
/org
/pf
/utils
- ImageResources.java
... a bunch of other classes
file: org.pf.utils.ImageResources
package org.pf.utils;
import javax.swing.*;
import java.awt.event.*;
import java.lang.reflect.*;
/**
* <p>Title: ImageResource Util</p>
* <p>Description: Central class for grabbing images.</p>
* <p>Copyright: Copyright (c) 2003, 2004</p>
* <p>Company: Reverse Alchemy, Inc.</p>
* @author PF
* @version 1.0
*/
public class ImageResources {
public final static Icon smallLogo =
new ImageIcon(
ImageResources.class.getResource(
"../../../images/small-logo.png"));
public final static Icon largeLogo =
new ImageIcon(
ImageResources.class.getResource(
"/images/large-logo.png"));
private ImageResources() {
}
/** Just a small test program to ensure
* that the images are loadable.
*
* @param args String[]
*/
public static void main(String[] args) {
JFrame f = new JFrame();
JPanel panel = new JPanel();
f.getContentPane().add(panel, null);
panel.setLayout(new BoxLayout(panel, 1));
try {
Class c =
Class.forName(
"org.pf.utils.ImageResources");
Field [] fields = c.getFields();
for (int i = 0; i < fields.length; i++) {
panel.add(new JLabel(fields[i].getName(),
(ImageIcon)fields[i].get(null), JLabel.HORIZONTAL));
panel.add(Box.createVerticalStrut(16));
}
}
catch (Exception ex) {
ex.printStackTrace();
}
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.pack();
f.show();
}
}
Note that if you don't get the above code to
work, check your directories. Also, I made
some modifications from an existing class
that I use, notably the package structure.
So the "../../../" in the ImageResources
might be wrong. But it might be right.
Make sure you do have the images in the
right place first.
>
> What you need to do is find some class in a parent directory (or create a
> dummy one) and use it to load the resources. In your case, create a class
> called com.mycompany.ImageLoader and use it as follows:
>
> jButton1.setIcon(new
> ImageIcon(ImageLoader.class.getResource("myimages/myPicture.PNG")));
>
> It does mean modifying JBuilder's generated code, but once you've done so
> once JBuilder should leave it alone.
What I do is to create a private member Icon variable which
is then usable in the Designer. For instance, ...
public class AnExample {
private Icon smallLogo = ImageResources.smallLogo;
private Icon largeLogo = ImageResources.largeLogo;
...
}
Now, in the Designer, when you go to the "icon"
property of say a JButton, you will see
<none>
smallLogo
largeLogo
in the drop-down list. Pick one and the Designer shows
the button with the icon on it ... that is, if you
have compiled the project in order to get the images
copied (deployed) into the /classes directory.
No need for messing around with paths. And if
you use the "Resource Strings" Wizard on the
ImageResources class, you can even modify the
location (or internationalize) of your images
using .properties files. For example, suppose
you need to show the choking symbol -- for
the US, you'd show an icon with arms crossed
over the chest, left-hand to the right side of
the neck, and vice versa for the right hand.
For the international sign, you'd show an icon
with arms raised, hands inverted and gripping the
neck from their respective sides. In both
cases, the tongue would be out, and eyes
would be bulging.
Any smileys needed?
--
Paul Furbacher (TeamB)
Save time, search the archives:
http://www.borland.com/newsgroups/ngsearch.html
Is it in Joi Ellis's Faq-O-Matic?
http://www.visi.com/~gyles19/fom-serve/cache/1.html
Finally, please send responses to the newsgroup only.
That means, do not send email directly to me.
Thank you.
Technical Details
Message-ID:
<3ffc2c70@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<3ffb2887$3@newsgroups.borland.com> <3ffc1c93$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,ed0e58961208387c X-Google-Attributes: gid111d45,public X-Google-ArrivalTime: 2004-01-07 08:09:44 PST Date: Wed, 07 Jan 2004 10:57:33 -0500 From: Paul Furbacher <pfurbacher@mac.com> User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.6b) Gecko/20031205 Thunderbird/0.4 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing Subject: Re: How to use images from up directories? References: <3ffb2887$3@newsgroups.borland.com> <3ffc1c93$1@newsgroups.borland.com> In-Reply-To: <3ffc1c93$1@newsgroups.borland.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 68.39.103.120 Message-ID: <3ffc2c70@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1073491056 68.39.103.120 (7 Jan 2004 07:57:36 -0700) Lines: 172 Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail