How to Display a Webpage Inside a Swing Application
In this tutorial, we are going to see how to display a webpage inside a swing application. JEditorPanel class is used to create a simple text editor. This class provides the method setContentType() and setText().
- setContentType(‘text/plain’): This method is used to set the content type, here text/plain means plain text.
- setText(text): This method is used to set the initial text.
Java Program to Display a Webpage Inside a Swing Application:
import javax.swing.*; import java.io.*; public class DisplayWebPage extends JFrame { public static void main(String[] args) { JEditorPane editor = new JEditorPane(); editor.setEditable(false); try { editor.setPage("https://example.com/"); }catch (IOException e) { editor.setContentType("text/html"); editor.setText("Page could not load"); } JScrollPane scrollPane = new JScrollPane(editor); JFrame f = new JFrame("Display example.com web page"); f.getContentPane().add(scrollPane); f.setSize(700,400); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
Output: