Relative path in JLabel HTML
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();
}
}
Use this function to prepare the html text for the JLabel to display images relative to the package of a class.
public static String prepareHtmlToJLabelText(Class relativeClass, String html) {
Pattern p = Pattern.compile("src=['\"](.*?)['\"]");
Matcher m = p.matcher(html);
while (m.find()) {
html = html.replace(m.group(), "src='" + relativeClass.getResource(m.group(1)) + "'");
}
return html;
}
The function replace the content of the "src" attribute to make it relative to the provider class.
Example:
jLabel.setText(prepareHtmlToJLabelText(this.getClass(), "<html><div style='text-align: center;'><img src='imageA.png'></div>Bla bla bla...<div style='text-align: center;'><img src='imageB.png'></div>"));
Anyway for a real html support use JEditorPane.
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>";
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.
i just replaced your
<img src=\"file://s.png\">\n"
//with
<img src=\"file:///s.png\">\n"+
and that solved the problem
NOTE: i placed the s.png file in the src/java folder
精彩评论