JClass Elements

PreviousNextIndex

8

HTML/Help Panes

Features of JCHTMLPane  Features of JCHelpPane  Classes  Properties 

Constructors and Methods  Examples 


8.1 Features of JCHTMLPane

JCHTMLPane is a subclass of Swing's JEditorPane which has been hard-coded to use the HTML Editor kit. HTML display can be as simple as passing the HTML code to JCHTMLPane's setText() method. Alternatively, you can pass the text as a parameter to the constructor. This class also implements a Hyperlink listener to implement link traversal and different cursor images (hand cursor and wait cursor).

JCHTMLPane is an extension of JEditorPane that lets you:

Note that the HTML functionality in Swing's JEditorPane is based on javax.swing.text.html.HTMLEditorKit, which supports most, but not all, HTML 3.2 tags. The APPLET tag is not supported (March, 2000), and care should be taken when using OBJECT, SCRIPT, FRAME, and dynamic HTML.


8.2 Features of JCHelpPane

JCHelpPane is an extension of JCHTMLPane in that it contains two JCHTMLPanes under a header pane. A typical use places an HTML page containing a title in the header pane, a table of contents page on the left, and a contents page on the right. You can use it to provide your users with a lightweight browser for a HTML-based help facility.


8.3 Classes

JCHTMLPane provides all the functionality necessary for an HTML-based pane, while JCHelpPane implements a lightweight two- or three-paned help system. Both of these are JavaBeans.

Figure 32 :  JCHTMLPane inherits from JEditorPane.

Figure 33 :  JCHelpPane inherits from JSplitPane.


8.4 Properties

JCHTMLPane's properties are the same as JEditorPane's. The class behaves like a JEditorPane with extra HTML awareness.

For a full listing of JCHTMLPane's properties, see Appendix A, Bean Properties Reference.


8.5 Constructors and Methods

8.5.1 Constructors

Constructors for JCHTMLPane

Along with the parameterless constructor for creating a blank pane, two others provide a handy way of instantiating a pane and providing it with HTML content in one operation.

Constructor

Description

JCHTMLPane()

Constructs a blank HTML pane.

JCHTMLPane(URL url)

Constructs an HTML pane with the specified URL.

JCHTMLPane(String text)

Constructs an HTML pane with the specified HTML text.

Constructors for JCHelpPane

JCHelpPane's constructors let you specify source pages using URLs or Strings. The latter may be advantageous if you generate some HTML-formatted text dynamically.

Constructor

Description

JCHelpPane()

Constructs a single blank "contents" pane.

JCHelpPane(URL contents, URL view)

Constructs, from the specified URLs, a help screen with a contents pane on the left and a view pane on the right. Note that Strings may be used in place of URLs.

JCHelpPane(URL contents, URL view, URL title)

Constructs, from the URLs, a help screen with three frames: a header frame that spans the top of the window, and two side-by-side frames underneath. Note that Strings may be used in place of URLs.

8.5.2 Methods

JCHTMLPane

The method of note is setText(), which is inherited from JEditorPane. Use it to pass text with embedded HTML tabs to the JCHTMLPane. An alternative way to pass the text is to via the pane's constructor, described above.

JCHelpPane

Although it is possible to construct a help browser using just the constructors for JCHelpPane, it has a number of methods are provided that may help your construction:

getContentsPage()
setContentsPage()

Gets or sets the contents page. That is, get or set the HTML pane on the left hand side.

getContentsPane()

Returns the HTML pane on the left hand side.

getTitlePage()
setTitlePage()

Gets or sets the title page. That is, gets or sets the HTML pane at the top.

getViewPage()
setViewPage()

Gets or sets the view page. That is, gets or sets the HTML pane on the right hand side.

getViewPane()

Returns the HTML pane on the right hand side.

isUseToolBar()

setUseToolBar()

Gets or sets the value of useToolBar.

If the set method returns true, the component traverses up the tree to find its root pane container and adds a tool bar to it if one does not exist. If one exists, it adds the HTML navigation buttons to the existing toolbar. If two buttons exist in the tool bar named Back and Forward, then it will not add the buttons, but rather add listeners to those buttons.


8.6 Examples

JCHTMLPane

The following incomplete code fragment shows how you can compose your HTML text dynamically, then pass it to an instance of JCHTMLPane. The result is shown in the accompanying figure.

String myHTMLText = "<HTML><HEAD><TITLE>JCHTMLPane Demo</TITLE></HEAD>";
myHTMLText += "<BODY><B>HTML (Bold) <P> <H1>JCHTMLPane understands basic HTML tags,</H1>";
myHTMLText += "such as headings:";
myHTMLText += "<H2 COLOR=red>A second level heading.</H2>";
myHTMLText += "<H3 COLOR=blue><EM>And lists:</EM></H3><BR>";
myHTMLText += "<OL><LI>Life is like a box of choco-lates";
myHTMLText += "<LI>Judy, Judy, Judy";
myHTMLText += "<LI>Play it again, Sam</OL>";
myHTMLText += "<A HREF=\"http://www.quest.com\">And links to other Web pages</A>";
myHTMLText += "<P>Tables too!<TABLE BORDER=10 BORDERCOLOR=BLACK BGCOLOR=WHITE>";
myHTMLText += "<tr><td>ROW ONE, First COLUMN cell</TD>
<TD>ROW ONE, Second COLUMN Cell</TD>
<TD>ROW ONE, Third COLUMN cell</TD></TR>";
myHTMLText += "<tr><td>ROW TWO, First COLUMN cell</TD>
<TD>ROW TWO, Second COLUMN Cell</TD>
<TD>ROW TWO, Third COLUMN cell</TD></TR>";
myHTMLText += "<tr><td>ROW THREE, First COLUMN cell</TD>
<TD>ROW THREE, Second COLUMN Cell</TD>
<TD>ROW THREE, Third COLUMN cell</TD></TR>";
myHTMLText += "</TABLE>";
myHTMLText += "</BODY></HTML>";

JCHTMLPane pane = new JCHTMLPane(myHTMLText);
pane.setEditable(false);
pane.setVisible(true);
frame.getContentPane().add(pane, BorderLayout.SOUTH);

frame.pack();
frame.setVisible(true);

Figure 34 :  A JCHTMLPane whose contents are derived from HTML Strings in the class.

JCHelpPane

This example demonstrates instantiating JCHelpPanes with both Strings and URLs. If any of the URLs can't be found, the version of JCHelpPane that uses Strings is displayed.

import com.klg.jclass.util.swing.*;
import com.klg.jclass.util.value.*;
import javax.swing.*;
import java.awt.*;

/**
* This example demonstrates the use of a JCHelpPaneExample
*/
public class HelpPaneExample {

// All the work is done in main()

public static void main(String args[]) {
String contents = new String("The contents pane of the JCHelpPane if URL isn't found.");
String view = new String("The view pane of the JCHelpPane if URL isn't found.");
String title = new String("Header for the Help Pane.");
JFrame frame = new JCExitFrame("Help Pane Example");
JCHelpPane app = new JCHelpPane(contents, view, title);
try {
java.net.URL contentsFromURL = new
java.net.URL("http://....../toc_page.html");
java.net.URL viewFromURL = new
java.net.URL("http://....../readme.html");
java.net.URL titleFromURL = new
java.net.URL("http://....../jclasslogo.html");
app = new JCHelpPane(contentsFromURL, viewFromURL, titleFromURL);
}
catch (java.net.MalformedURLException e) {
System.out.println("Malformed URL");
}

app.setPreferredSize(new Dimension(640, 400));
frame.getContentPane().add(app);
frame.pack();
frame.setSize(700, 450);
frame.show();
}

}

Figure 35 :  A JCHelpPane showing the HTML version of this manual.

Figure 36 :  In this example, the alternate JCHelpPane when the URL can't be found.


PreviousNextIndex