Java Swing How to - Show webpage in JEditorPane








Question

We would like to know how to show webpage in JEditorPane.

Answer

import java.io.IOException;
/*from w  ww. jav  a 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 jep = new JEditorPane();
    jep.setEditable(false);

    try {
      jep.setPage("http://www.google.com");
    } catch (IOException e) {
      jep.setContentType("text/html");
      jep.setText("<html>Could not load http://www.google.com </html>");
    }

    JScrollPane scrollPane = new JScrollPane(jep);
    JFrame f = new JFrame();

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(scrollPane);
    f.setSize(512, 342);
    f.show();

  }

}