🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
5 messages
5 total messages Started by Rocket Tue, 06 Jan 2004 21:28
How to use images from up directories?
#1320
Author: Rocket
Date: Tue, 06 Jan 2004 21:28
41 lines
1964 bytes
Hi, sorry my bad english,

I have problems with images and directories in JBuilder.

If I have an image in directory below from current directory, Jbuilder
create an code like this for an Icon:

jButton1.setIcon(new
ImageIcon(Frame1.class.getResource("img/myPicture.PNG")));

OK, it�s works... :-|

But, if I have the images in above directories, JBuilder creates a code like
this:

jButton.setIcon(new ImageIcon(new
java.net.URL("file:///C:/projects/mysystem/src/com/mycompany/myimages/myPict
ure.PNG")));

In other words, it put full path for the image.

My system like is:

package com.mycompany.mysystem.ihc
public class MyFrame extends JFrame {
...
}

...And the images are in package com.mycompany.myimages. (2 up�s
directories)

Using Design, the JBuilder creates an code like this:
...
jLabel1.setIcon(new ImageIcon(new
java.net.URL("file:///C:/projects/mysystem/src/com/mycompany/myimages/myPict
ure.PNG")));
...

Obviously, this code will not function at client...

How I use the Design (and configurate paths...) to create a correct code?
Re: How to use images from up directories?
#1322
Author: Kevin Dean
Date: Wed, 07 Jan 2004 14:48
71 lines
3140 bytes
I ran into similar problems some time ago.  The issue is that Java doesn't
support ".." directories when locating resources.

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.

-- 
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/

"Rocket" <rocket@via.com.br> wrote in message
news:3ffb2887$3@newsgroups.borland.com...
> Hi, sorry my bad english,
>
> I have problems with images and directories in JBuilder.
>
> If I have an image in directory below from current directory, Jbuilder
> create an code like this for an Icon:
>
> jButton1.setIcon(new
> ImageIcon(Frame1.class.getResource("img/myPicture.PNG")));
>
> OK, it�s works... :-|
>
> But, if I have the images in above directories, JBuilder creates a code
like
> this:
>
> jButton.setIcon(new ImageIcon(new
>
java.net.URL("file:///C:/projects/mysystem/src/com/mycompany/myimages/myPict
> ure.PNG")));
>
> In other words, it put full path for the image.
>
> My system like is:
>
> package com.mycompany.mysystem.ihc
> public class MyFrame extends JFrame {
> ...
> }
>
> ...And the images are in package com.mycompany.myimages. (2 up�s
> directories)
>
> Using Design, the JBuilder creates an code like this:
> ...
> jLabel1.setIcon(new ImageIcon(new
>
java.net.URL("file:///C:/projects/mysystem/src/com/mycompany/myimages/myPict
> ure.PNG")));
> ...
>
> Obviously, this code will not function at client...
>
> How I use the Design (and configurate paths...) to create a correct code?
>
>
Re: How to use images from up directories?
#1323
Author: Paul Furbacher
Date: Wed, 07 Jan 2004 15:57
172 lines
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.
Re: How to use images from up directories?
#1331
Author: Kevin Dean
Date: Wed, 07 Jan 2004 21:45
191 lines
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.
Re: How to use images from up directories?
#1358
Author: John McGrath [TeamB]
Date: Tue, 20 Jan 2004 00:40
16 lines
1464 bytes
On 1/7/2004 at 4:45:15 PM, Kevin Dean wrote:

> 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.

I think that is the case.  I recall having problems using ".." some
time ago, but I have since tested it and found that it works.

-- 
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads