Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Display html contents with java

In this article we are going to implement a program which is capable of displaying the html content on a simple java frame.

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.

Program Explanation:

First we will include the import classes which are needed for the program to work.

Listing1: Including the import classes

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.

Listing2: 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:

Listing3: Defining 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 :

  1. First arguments tell about the type of data. Since we have to display the html data so we set the arguments as “text/html”
  2. The second argument will take the html content. Since we stored that in the html named variable so we passed the html variable as the second argument there.

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.

Optional :

If you want to show the contents from some URL instead from the offline html contents then you may do the following:

Listing4: Optional

	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:

Displaying html with java

Full Source code :

Listing5: 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.



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

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

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login