Use JTextPane to display a web page from a URL in Java
Description
The following code shows how to use JTextPane to display a web page from a URL.
Example
import java.awt.Dimension;
/* w w w. j a v a 2s. c om*/
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
public class Main {
public static void main(String[] args) throws Exception {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane textpane = new JTextPane();
textpane.setContentType("text/html");
textpane.setEditable(false);
textpane.setPage("http://java2s.com");
textpane.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
f.add(new JScrollPane(textpane));
f.setSize(new Dimension(380, 320));
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »