Search code examples
javaswingjeditorpane

JEditorPane: External resources aren't resolved


I'm using a JEditorPane to display some HTML data, however any images that have a src="..." at another location / server isn't displayed. I am guessing that this is some kind of security constraint; my question is: can I display data from outside of the JEditorPanes URL and if so how?

enter image description here

If there aren't any ways of disabling this, what would be a better method of being able to resolve these external resources while displaying the HTML in an AWT/SWING environment?

Code:

File f = new File("index.html");
JEditorPane jep = new JEditorPane(f.toURI().toURL());
JScrollPane sp = new JScrollPane(jep);

JFrame frame = new JFrame();
frame.add(sp);
jep.setEditable(false);

frame.setVisible(true);
frame.setSize(500, 500);
frame.setTitle(wpj.getParse().getTitle());

Thanks to Andrew Thompson for highlighting an error in my HTML code. I've updated the HTML which can now be found here: http://pastebin.com/EixG3WLH -- It appears that any improperly formed HTML can cause the images to not appear.


Solution

  • It seems to work fine here using a file based URL for the HTML and with the image at imgur.com.

    ForeignImageInPane

    import java.awt.*;
    import javax.swing.*;
    import java.io.File;
    
    class ForeignImageInPane {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    File f = new File("index.html");
                    try {
                        JEditorPane jep = new JEditorPane(f.toURI().toURL());
    
                        JScrollPane sp = new JScrollPane(jep);
                        sp.setPreferredSize(new Dimension(400,200));
    
                        JOptionPane.showMessageDialog(null, sp);
                    } catch(Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    

    index.html

    <html>
    <body>
    <img src='https://i.sstatic.net/WeiWi.png'>
    </body>
    </html>
    

    What are the actual URLs of the images?

    Update

    It also seems to work with an URL taken directly from the HTML. Vis.

    <html>
    <body>
    <img src='http://upload.wikimedia.org/wikipedia/commons/thumb/a/af/Kit_shorts.svg/100px-Kit_shorts.svg.png'>
    </body>
    </html>