Java Program to display a webpage in JEditorPane

Java 8Object Oriented ProgrammingProgramming

Use the setPage() method of the JEditorPane to display a webpage:

JEditorPane editorPane = new JEditorPane();
editorPane.setPage("https://www.tutorialspoint.com");

The following is an example to display a webpage in JEditorPane:

Example

import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
public class SwingDemo {
   public static void main(String[] args) {
      JEditorPane editorPane = new JEditorPane();
      try {
         editorPane.setPage("https://www.tutorialspoint.com");
      } catch (IOException e) {
         editorPane.setContentType("text/html");
         editorPane.setText("<html>Connection issues!</html>");
      }
      JScrollPane pane = new JScrollPane(editorPane);
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(pane);
      frame.setSize(500, 300);
      frame.setVisible(true);
   }
}

Output

raja
Published on 02-May-2019 10:35:21
Advertisements