Java Swing How to - Load HTML with URL resource image to JEditorPane








Question

We would like to know how to load HTML with URL resource image to JEditorPane.

Answer

import java.awt.BorderLayout;
/*from  w  w  w .ja  va  2  s  . c  o  m*/
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

public class Main {

  public static void main(String[] args) {
    JEditorPane htmlPane = new JEditorPane();
    String description = "<html><body>Hello<table border=1>"
        + "<tr><td><img alt='Bad' src='http://www.java2s.com/style/download.png'/></tr></td></table></body></html>";
    htmlPane.setContentType("text/html");
    htmlPane.setText(description);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new JScrollPane(htmlPane));
    frame.pack();
    frame.setVisible(true);
  }

}