showing images on jeditorpane (java swing)
Asked Answered
O

5

9

I have a JEditorPane created by this way:

JEditorPane pane = new JEditorPane("text/html", "<font face='Arial'>" + my_text_to_show + "<img src='/root/img.gif'/>" + "</font>");

I put this pane on a JFrame.

Text is shown correctly, but I can't see the picture, there is only a square indicating that there should be an image (i.e.: "broken image" shown by browsers when picture has not been found)

Oxygenate answered 21/2, 2009 at 13:58 Comment(0)
E
9

You have to provide type, and get the resource. That's all. My tested example, but I'm not sure about formating. Hope it helps:

import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;

public class Test extends JFrame {

    public static void main(String[] args) throws Exception {
        Test.createAndShowGUI();
    }

    private static void createAndShowGUI() throws IOException {

        JFrame.setDefaultLookAndFeelDecorated(true); 

        JFrame frame = new JFrame("HelloWorldSwing");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        String imgsrc = 
            Test.class.getClassLoader().getSystemResource("a.jpg").toString();
        frame.getContentPane().add(new JEditorPane("text/html",
            "<html><img src='"+imgsrc+"' width=200height=200></img>"));
        frame.pack();

        frame.setVisible(true);
    }
}
Educational answered 21/2, 2009 at 15:28 Comment(4)
I've already tried this way and I ever get a null pointer exception :(Oxygenate
I don't know where you got the exception,but you can try my example. And put a.jpg into the same directory with this class. you can compile it as javac Test.java && run java Test. And because I haven't set the size of the window you can set it larger to see image.Educational
(ClassLoader.getSystemResource is a static method. getResource would be a better choice.)Brigand
This solution requires the code to calculate all the references. @john gardener's solution seems more abstract but allows direct HTML editing with relative links.Horsewoman
B
5

The JEditorPane is using HTMLDocument.getBase to locate relative urls as well, so if you are displaying content from a directory, make sure to set the base on the html document so it resolves urls relative to the base directory.

Depending on where that image actually is, you might want to extend HTMLEditorKit+HTMLFactory+ImageView and provide a custom implementation of ImageView, which is responsible for mapping the attribute URL to the image URL, too.

Bolide answered 21/2, 2009 at 21:42 Comment(0)
M
3

None of the above worked for me, however 'imgsrc = new File("passport.jpg").toURL().toExternalForm();' let me to try and have each image in the html have a preceding 'file:' so that it now reads:

<img src="file:passport.jpg" />

And that works fine for me.

Merralee answered 10/4, 2012 at 6:58 Comment(0)
L
1

If you want to specify relative path to the image.

Let's say your project folder structure is as following:

sample_project/images
sample_project/images/loading.gif
sample_project/src
sampler_project/src/package_name

Now the image tag would look like this:
"<img src='file:images/loading.gif' width='100' height='100'>"

Yaay!

Linis answered 30/11, 2013 at 14:39 Comment(0)
A
0

I used this when I was working in netbeans, it worked though. I think a little modification if the program should run outside of netbeans,

String imgsrc="";
try {
    imgsrc = new File("passport.jpg").toURL().toExternalForm();
} catch (MalformedURLException ex) {
   Logger.getLogger(EntityManager.class.getName()).log(Level.SEVERE, null, ex);
}
//System.out.println(imgsrc); use this to check
 html = "<img src='" + imgsrc + "' alt='' name='passport' width='74' height='85' /><br />";
//use the html ...   

if you run from the jar, the image file has to be on the same directory level, ... in fact, the image file has to be on the same directory as your execution entry.

Actable answered 15/8, 2011 at 10:50 Comment(1)
"if you run from the jar, the image file has to be on the same directory level,.." Rot! See the sources on this answer that demonstrates how to load images by relative references, as well as how to use use the base element in the HTML. The base can also be set explicitly in the JEditorPane methods.Goethe

© 2022 - 2024 — McMap. All rights reserved.