views:

508

answers:

4

Hi, I am trying to make JLabel show an html which is referencing an image using a relative path. But I cannot make JLabel locate the image. It works fine when I am using absolute path.

I have tried running the program from the command line or from eclipse and add dialog to show me where is the current working directory but for avail. I have therefor came to the conclusion that the image is not searched in the current directory - which brings me to the point. where is the image looked for?

here is a test code that show what I am doing:

import javax.swing.*;

public class HTMLLabel extends JFrame {
    public HTMLLabel() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JOptionPane.showMessageDialog( this, 
          System.getProperty("user.dir"));

         String html = "<html>\n" +
     " <body>\n" +
     "  <div style=\"text-align: center;\">\n" + 
     "   <img src=\"file://s.png\">\n"+
     "  </div>\n"+
     " </body>\n"+
     "</html>";

         JLabel label = new JLabel(html);
         add(label);

         pack();
         setVisible(true);
    } 

    public static void main(String[] args) {
         new HTMLLabel();
    }
}
+2  A: 

Why are you even doing it like this? Just use this JLabel(Icon image) constructor

JLabel label = new JLabel(createImageIcon("s.png","description"));

protected ImageIcon createImageIcon(String path, String description) {
  java.net.URL imgURL = getClass().getResource(path);
  if (imgURL != null) {
    return new ImageIcon(imgURL, description);
  } else {
    System.err.println("Couldn't find file: " + path);
    return null;
  }
}


Or if you insist on the html variant.

btw The file protocol uses 3 slashes (file://s.png is invalid) and file:///s.png would mean C:\s.png. If the image resides in the starting directory you could use.

String path = System.getProperty("user.dir");
String html =
  "<html>\n" +
     "<body>\n" +
        "<div style=\"text-align: center;\">\n" +
          "<img src=\"file:///"+path+"/s.png\">\n"+
        "</div>\n"+
     "</body>\n"+
  "</html>";

But I make no guarantees about the 2nd solution.

jitter
jitter, The reason I don't directly set the icon for the label is because there are two different programs involved here one that choose the html and one for showing it.and that is why I cannot use any of your solution (easily). but thanks (:
kroiz
+1  A: 

I see two variants:

I don't know why but for me this works

"                       <img src=\"file:s.png\">\n"+

assuming that s.png is in current working directory.

Another variant that seems more appropriate for me is:

URL url = HTMLLabel.class.getResource( "/s.png" );
  String html = "<html>\n" +
    "       <body>\n" +
    "               <div style=\"text-align: center;\">\n" + 
    "                       <img src=\""+url+"\">\n"+
    "               </div>\n"+
    "       </body>\n"+
    "</html>";
Interesting I guess java automatically corrects the incomplete scheme + path to file:/// but fails on file://s.png
jitter
It's really strange because java also corrects doubling of path separators (e.g. /gif//s.png and /gif///s.png to /gif/s.png )
contexte, indeed it works with <img src=\"file:s.png\">\n" +which is very strange but unfortunately I need it in a specific folder under the working directory and <img src=\"file:subdir/s.png\">\n" +Does not work.About the second solution you and jitter suggest, it is a bit of a problem, because the html is not hardcoded in the program that actually put it in the JLabel - it is created by another program and placed in a subdir of the working directory. It would require me to parse the html and replace the image src attribute with a absolute path - something I with to avoid.
kroiz
and have you tried file:/// like jitter suggested?
Yeah, that did not help either.Very puzzling...
kroiz
Do you think it could be a platform thingy? I am running ubuntu.
kroiz
Look, I'm also working on Ubuntu. 8.10 to be precise. This works for me: <img src=\"file:subdir/s.png\"> . Where subdir is under current wirking directory.
May be you face some other problem? For example wrong case of folder names...
contexte, are you using openJDK or sun's java, maybe the difference comes from there.
kroiz
no, I use Sun JDK version 1.6.0_10
This is my test - http://rapidshare.com/files/296357866/test_label.zip.html . Download, extract and run "java HTMLLabel". For me it works. I don't know how else I can help you:(
contexte, Thank you so very very much. it works now. you were right all along. I don't know what stupid mistake I have done. but now it works as you suggested from the start.I don't believe I wasted days on this....Thanks again for your persistence and dedication to help me.BTW the project you helped me with is an open source project which is hosted on source forge:http://sourceforge.net/projects/periman/
kroiz
Great information contexte.
James P.
A: 

I found that on windows the path can indeed be set to relative using something like that:

<img src=\"file:subdir/s.png\">

But on ubuntu that does not work! It only works w/o the subdir. Does anyone has any idea how can I write the img src to have a relative cross platform path? Pleeeaase....

kroiz
not really:"Write Once, Run Anywhere(tm)"Is it?!
kroiz
Is the sun implementation of Java is open source? All I could find is OpenJDK.
kroiz
A: 

Thanks a ton.. Last couple of hrs i had spent for googling this solution and at last file:images/image.png works, thank you again

prashant