Java Reference
In-Depth Information
constructor. A URL is a web address. One can use class URL for this purpose, or
one can just pass on the URL as a string. The constructors have the following
form:
JEditorPane(URL webAddress)
JEditorPane(String webAddressAsString)
These constructors have to be embedded into a try-catch block, because gener-
ating a URL can throw an exception. To access a local file, the URL has to start
with file:/// instead of http:// .Weuse a local file in our example because
then the program works without a connection to the Internet. If the computer is
connected to the Internet, one can use the following URL:
http://www.imm.dtu.dk/swingbook/HTMLTest/test1.html
We are only interested in displaying HTML and do not allow the text to be edited
using method setEditable(false) of JEditorPane .
File: its/HTML/SimpleHTMLFrame.java
package its.HTML;
1.
2.
3.
import its.SimpleFrame.SimpleFrame;
import javax.swing.JEditorPane;
4.
5.
6.
public class SimpleHTMLFrame extends SimpleFrame
{
7.
8.
private JEditorPane ediPane;
9.
private static String htmlSource = "/its/TestData/test1.html";
10.
private static String workingDir;
11.
12.
public SimpleHTMLFrame(String URLname)
13.
{
14.
this .setSize(400,400);
15.
workingDir = System.getProperty("user.dir");
16.
// in the following we assume that the program
17.
// is run from the parent directory of its.
18.
// Use an absolute path if necessary:
19.
try {
20.
ediPane = new JEditorPane("file:///"+workingDir+htmlSource);
21.
// ediPane = new JEditorPane
(”file:///C:/absolute/path//its/TestData/
ediPane.setEditable( false );
22.
} catch (Exception e){}
23.
// Later, the code for the link listener will be added here.
24.
this .getContentPane().add(ediPane);
25.
}
26.
27.
Search WWH ::




Custom Search