Re: Help : ImageIcon constructor fails to load image




"AlecB" <nobodyhere@xxxxxxxxxx> wrote in message
news:dk2qel$ras$1@xxxxxxxxxxxxxxxxxx
> 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)
>
This link produces a '404' error when I attempt to view the page. Perhaps
the site is down temporarily?

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

Basically, the technique for getting an image out of a file in the
filesystem is slightly different than the technique for getting a jar entry
out of a jar in the classpath.

The code in your 'createImageIcon()' method is correct for getting a jar
entry out of a jar in the classpath. If your image is indeed in a jar on the
classpath, then you have nothing further to do.

However, if you want to get your image from a relative location within your
local filesystem, you need to do things just a little differently, something
like this (untested!):

String fileLocation = "images/icon.gif"; //location of file expressed as a
String
File imageFile = new File(fileLocation); //create a File reference to that
location
URL imageFileUrl = imageFile.toUR(); //create a URL pointing to the file
labels[1].setDisabledIcon(new ImageIcon(imageFileUrl)); //get the image from
its URL

Rhino


.



Relevant Pages

  • Re: Help : ImageIcon constructor fails to load image
    ... a strange problem. ... The problem is that when I attemp to instantiate an Imageicon with a ... filesystem is slightly different than the technique for getting a jar entry ... entry out of a jar in the classpath. ...
    (comp.lang.java.help)
  • Yet another way to use Java
    ... * @param mainClass e.g. FontShower, ... StompProject (String mainClass, ... * does this project have a jar ... void mkDescBtm() ...
    (comp.lang.java.programmer)
  • Re: how to set up a simple plugin mechanism
    ... > relevant classes or a list of names from which the relevant class ... > classpath or in some jar. ... WhatEverPlugin interface, and if they do I load them. ...
    (comp.lang.java.help)
  • Re: Suchreihenfolge =?ISO-8859-15?Q?f=FCr?= CLASS-Files
    ... Umgebungsvariable CLASSPATH eine aus einem JAR bestehende Anwendung ... Zielrechner eine alte Version meiner Bibliotheken installiert habe und im ... einer fremden Maschine alles installiert hat (meist nur die JVM mit ...
    (de.comp.lang.java)
  • Re: Java on VMS
    ... Maybe you just need to put a missing jar file in WEB-INF/lib. ... I have had some issues with getting jars to show on the classpath, ... I'm no VMS pro so I'm not entirely sure ...
    (comp.os.vms)