Search This Blog

How to Load a Webpage in a Java App

Do you wonder how to load a webpage in your java application? It is simple. You can load a web document whether it is a web document in internet or a web document from a file in the localhost. I know three methods to do it. You can use JTextPane in java swing (javax.swing), JEditorPane in java swing (javax.swing) or WebView in javaFX (javafx.scene.web.WebView). We will discuss each method to display a webpage in a java application.

The three methods we are going to discuss are:
  • Using JTextPane
  • Using JEditorPane
  • Using WebView in JavaFX



Loading Webpage Using JTextPane

The JTextPane class in java swing allows us to load a web document to it. To show a webpage in a JFrame or JPanel using JTextPane, you should use the setPage() method of JTextPane class. You can either load a website from internet or a web page (web document) from your localhost. We will first see how to load a webpage (website) to a JTextPane. The following java code will show you how to display a webpage in a java application using JTextPane.

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;

public class LoadWebPage extends JFrame
{

public LoadWebPage()
{
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(620,440);
JScrollPane sp=new JScrollPane();
JTextPane tp=new JTextPane();
tp.setEditable(false);
URL myurl = null;
try {
    myurl=new URL("http://javatongue.blogspot.com");
    } catch (MalformedURLException ex) {ex.printStackTrace();}
sp.setViewportView(tp);
add(sp);
try {
    tp.setPage(myurl);
    } catch (IOException ex) {ex.printStackTrace();}

}

public static void main(String args[])
{
new LoadWebPage().setVisible(true);
}

}


In the 18th line, a URL object myurl is created. In 23rd line, we called the setPage() method of the JTextPane object tp passing the url as a parameter to it. You may also directly pass the url as a string since there is another overloaded method definition which accepts web address as a string. The JTextPane is not so good to display dynamic webpages. It can only display basic webpages without much styling and scripting.

Now let us see how to load a web document from file into the JTextPane. The webpage here in this example is an html file.


try {
    File f=new File("D:\\sample.html");
    tp.setPage(f.toURI().toURL());
    } catch (Exception ex) {ex.printStackTrace();}


In this code, we converts the file to URI, then to URL and pass it to the setPage() method of the JTextPane object.

Loading Webpage using JEditorPane

The JEditorPane class in java swing also allow us to load a webpage into it. Using JEditorPane, you can load a webpage to a JFrame or a JPanel. Similar to the JTextPane, JEditorPane also have a setPage() method. The following java program will show you how to embed a webpage in a java application using JEditorPane.


import java.net.URL;
import javax.swing.*;

public class LoadWebPage extends JFrame
{

public LoadWebPage()
{
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(620,440);
JScrollPane sp=new JScrollPane();
JEditorPane editorPane = new JEditorPane();
editorPane.setEditable(false);
sp.setViewportView(editorPane);
add(sp);
try {
    editorPane.setPage(new URL("http://javatongue.blogspot.com"));
    } catch (Exception ex) {ex.printStackTrace();}
}

public static void main(String args[])
{
new LoadWebPage().setVisible(true);
}

}


Similar to first method (using JTextPane), we can also load a webpage from file (web document) in localhost machine. The following code fragment will give you an idea.


try {
    File f=new File("D:\\sample.html");
    editorPane.setPage(f.toURI().toURL());
    } catch (Exception ex) {ex.printStackTrace();}


Just like JTextPane, JEditorPane is also not good at displaying dynamic webpages. There comes the third method which uses Java FX.

Loading Webpage Using WebView in JavaFX

Compared to the first two ways of embedding a webpage in a java application program, this method using the WebView in JavaFX can load dynamic webpages. It can load webpages with styles (like css) and scripts (like javascript). We will see it through a simple example.


import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javax.swing.*;

public class LoadWebPage
{

public static void main(String args[])
{
final JFrame frame=new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(620,440);
final JFXPanel fxpanel=new JFXPanel();
frame.add(fxpanel);

Platform.runLater(new Runnable() {
@Override
public void run()
    {
    WebEngine engine;
    WebView wv=new WebView();
    engine=wv.getEngine();
    fxpanel.setScene(new Scene(wv));
    engine.load("http://javatongue.blogspot.com");
    }
    });
frame.setVisible(true);
}
}



While using Java FX along with Java swing, there are some limitations. But the advantage of using JavaFX along with java swing is that it can load almost all dynamic webpages. It will display the webpage well in your application. You cannot include the lines like WebView wv=new WebView(); in the event dispatch thread (EDT) of java swing. The GUI creation and ActionEvents are handled by the EDT (event dispatch thread). JavaFX data should be accessed only on the JavaFX User thread and not on Event Dispatch Thread (EDT). Whenever you must change JavaFX data, wrap your code into a Runnable object and call the Platform.runLater.

how to load, display, embed or show a webpage, website or web document in JFrame, JPanel or any Java application program. java code example
Webpage displayed inside java swing application using JavaFX WebView
To load a web page from file (web document in localhost machine), you may try using the java code like the following one.


  engine.load("file:///D:\\Java Tongue.html");

No comments:

Post a Comment