Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
    }
}
like image 321
kroiz Avatar asked Oct 20 '09 10:10

kroiz


People also ask

What is relative file path in HTML?

HTML Relative File Paths The path of the file that is relative to the current web-page’s file is specified. a. Same folder as the current web-page file For example, there is a folder ‘DataFlair’ that has an image ‘DataFlair.png’. b. Images folder in the current folder itself

How do I find the relative path of a picture?

A relative path is always relative to the root of the document, so if your html is at the same level of the directory, you’d need to start the path directly with your picture’s directory name: dot-slash (./) Dot (.) points to the same directory and the slash ( / ) gives access to it: Double-dot-slash (../)

What is an HTML file path?

HTML File Paths. A file path describes the location of a file in a web site's folder structure. File paths are used when linking to external files, like: Web pages; Images; Style sheets; JavaScripts

How to find the location of a file in HTML?

The location of the file is specified as per the structure of the web folder. External resources such as CSS files, script files, images, multimedia, etc. can be linked to an HTML document. A website has a folder structure and the file path is to describe the location of the file within that folder structure. 1. HTML Absolute File Paths


2 Answers

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.

like image 181
Daniel De León Avatar answered Oct 01 '22 22:10

Daniel De León


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>";
like image 31
user132371 Avatar answered Oct 01 '22 23:10

user132371