Article View: borland.public.jbuilder.java.swing
Article #1331Re: How to use images from up directories?
From: Kevin Dean
Date: Wed, 07 Jan 2004 21:45
Date: Wed, 07 Jan 2004 21:45
191 lines
6467 bytes
6467 bytes
I'll have to check my code again. I haven't touched that particular piece
of code since JDK 1.2 so the issue may be long dead.
--
Check out our latest white papers at
http://www.datadevelopment.com/papers/index.html
BladeNET Scores With Borland Enterprise Tools
Team Development with JBuilder and Borland Enterprise Server
Dolphin Data Development Ltd.
http://www.datadevelopment.com/
"Paul Furbacher" <pfurbacher@mac.com> wrote in message
news:3ffc2c70@newsgroups.borland.com...
> 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:
<3ffc7e7e$3@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.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> <3ffc2c70@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 14:10:23 PST From: "Kevin Dean" <NkOdSePaAnM@datadevelopment.com> Newsgroups: borland.public.jbuilder.java.swing References: <3ffb2887$3@newsgroups.borland.com> <3ffc1c93$1@newsgroups.borland.com> <3ffc2c70@newsgroups.borland.com> Subject: Re: How to use images from up directories? Date: Wed, 7 Jan 2004 16:45:15 -0500 Lines: 193 Organization: Dolphin Data Development Ltd. X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 24.57.150.243 Message-ID: <3ffc7e7e$3@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1073512062 24.57.150.243 (7 Jan 2004 13:47:42 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail