Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2005

Tip: Looking for answers? Try searching our database.

Help - Image icon

Thread view: 
AlecB - 30 Oct 2005 16:49 GMT
Hi,

I am attempting to familiarising myself with swing and have encountered
a strange problem.

The problem is that when I attemp to instantiate an Imageicon with a
relative filename. it fails.

e.g.
- images/
    - icon.gif
- ImageLabelExample.class

I suspect its something simple. Can someone point out what I'm missing?

Thanks.

Code snippet ... (from www.oreilly.com/swing2)

...

public static main(String[] args){
    JLabel[] labels = new JLabel[3];
    labels[0] = makeLabel(JLabel.TOP, JLabel.LEFT);
    labels[1] = makeLabel(JLabel.TOP, JLabel.CENTER);
    labels[2] = makeLabel(JLabel.TOP, JLabel.RIGHT);
    // disable label 0
    labels[0].setEnabled(false);
    // Fails to find the image, the image is in the .jar file
    //labels[1].setDisabledIcon(new ImageIcon("images/icon.gif"));
    // This works
      labels[1].setDisabledIcon(creatImageIcon("images/icon.gif",null));
    for(int i = 0; i < labels.length; i++)
        c.add(labels[i]);
    frame.setSize(350, 350);
    frame.setVisible(true);
}

protected static JLabel makeLabel(int vert, int horiz) {
    JLabel l = new JLabel("Smile", icon, SwingConstants.CENTER);
    l.setVerticalTextPosition(vert);
    l.setHorizontalTextPosition(horiz);
    l.setBorder(BorderFactory.createLineBorder(Color.black));
    return l;
}

// If I create the ImageIcon here, all is well
protected static ImageIcon createImageIcon(final String path, String
description) {
    java.net.URL imgURL = ImageLabelExample.class.getResource(path);
    System.err.println( imgURL );
    if (imgURL != null) {
        return new ImageIcon(imgURL, description);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}
hiwa - 30 Oct 2005 20:11 GMT
In JAR file, you can't use file name because only file available
is the JAR file itself. There are no other files unlsess you
deliberately provide them along with the JAR file.

Use getResource() method of Class class instead.
Roedy Green - 31 Oct 2005 10:03 GMT
> ImageIcon("images/icon.gif"));

that's a filename on the local file system relative to the current
directory whatever that happens to be.  

Is that what you meant?

Try some code like this:

System.out.println( new File("images/icon.gif").getCanonicalPath() );

To see what file you are trying to get.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Fahd Shariff - 31 Oct 2005 17:37 GMT
You will have to try something along the lines of:

icon = new ImageIcon(getClass().getResource("images/icon.gif")) ;

Signature

Fahd Shariff

Raja - 02 Nov 2005 18:02 GMT
This is because the image is in the jar file and the gif file itself is
not present anywhere.
The other one works because the jar file might be in your classpath and
so getResource() finds it.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.