I am trying to make JLabel show an html that refers to an image using a relative path. But I can not get JLabel to find the image. It works great when I use the absolute path.
I tried to run the program from the command line or from eclipse and add a dialog to show me where the current working directory is, but for avail. I came to the conclusion that the image is not visible in the current directory, which brings me to the point. where to look for the image?
here is the test code that shows 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();
}
}
kroiz source
share