Java Program to display a webpage in JEditorPane


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

Updated on: 30-Jul-2019

463 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements