Suppose you have saved a webpage from the internet and you want to display the same in java then you may use this program as reference. This program is capable of showing the html contents on a simple java jframe. We will be making use of JEditorPane for fulfillment of our purpose.
First we will include the import classes which are needed for the program to work.
import javax.swing.JEditorPane; import javax.swing.JFrame;
Here, the JEditorPane is responsible for showing of the html content. It allows us to see the html content from a specific website or even from a html file on local computer.
JFrame is used to display a frame and all other components are included in this jframe. Now we define the class along with the main method.
public class HtmlContent extends JFrame
{
public static void main(String args[])
{
new HtmlContent().start();
}
The name of the class is HtmlContent, a main method is defined which will call the start method, which contains all the logic.
Now we will define the start method:
void start()
{
try
{
String html;
html="<html><head><title>Simple Page</title></head>";
html+="<body bgcolor='#777779'><hr/><font size=50>This is Html content</font><hr/>";
html+="</body></html>";
JEditorPane ed1=new JEditorPane("text/html",html);
add(ed1);
setVisible(true);
setSize(600,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Some problem has occured"+e.getMessage());
}
}
}
In the code above we defined a variable named html which will store the html content which we want to show on our Frame.
Now we input some html content inside the html variable. The html content we gave should display “This is Html content” and we will define the JEditorPane.
This JEditorPane takes 2 arguments :
Now we make use of the add method of JFrame to add it on the Jframe.
We set the frame as visible and the size of frame is set to be (600,600) which set the frame width and height. We set the default close operation as EXITONCLOSE so that when a user close the application then the program dies.
We kept the whole logic inside a try-catch block so that all exception can be caught and reported to user.
If you want to show the contents from some URL instead from the offline html contents then you may do the following:
URL u=new URL("http://www.mysite.com");
JEditorPane ed2=new JEditorPane(u);
In the code above we defined the Url which we want to display on our JFrame. We use the JEditorPane which takes its arguments as the URl object and now you can show the content of your site on the frame.
Output:

Full Source code :
import javax.swing.JEditorPane;
import javax.swing.JFrame;
public class HtmlContent extends JFrame
{
public static void main(String args[])
{
new HtmlContent().start();
}
void start()
{
try
{
String html;
html="<html><head><title>Simple Page</title></head>";
html+="<body bgcolor='#777779'><hr/><font size=50>This is Html content</font><hr/>";
html+="</body></html>";
JEditorPane ed1=new JEditorPane("text/html",html);
add(ed1);
setVisible(true);
setSize(600,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Some problem has occured"+e.getMessage());
}
}
}
Hope you liked the article. See you on the next article.








See the prices for this post in Mr.Bool Credits System below: