![]() ![]() ![]() |
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'sJEditorPane
which has been hard-coded to use the HTML Editor kit. HTML display can be as simple as passing the HTML code toJCHTMLPane
'ssetText()
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 ofJEditorPane
that lets you:
- Construct an HTML pane, given a URL.
- Construct an HTML pane, given a pointer to HTML text.
- Change the icon for the cursor when it is over a link.
- Follow the reference in a link.
- Use an
MDIMenuBar
andMDIToolBar
in addition to aJMenuBar.
Note that the HTML functionality in Swing's
JEditorPane
is based onjavax.swing.text.html.HTMLEditorKit
, which supports most, but not all, HTML 3.2 tags. TheAPPLET
tag is not supported (March, 2000), and care should be taken when usingOBJECT
,SCRIPT
,FRAME
, and dynamic HTML.
8.2 Features of JCHelpPane
JCHelpPane
is an extension ofJCHTMLPane
in that it contains twoJCHTMLPane
s 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.
- The lightweight browser becomes part of your application.
- Once links have been followed, forward and back buttons allow users to retrace their steps.
JCHelpPane
checks to see if a URL for the title pane was specified. If it wasn't, the title pane is not shown.
8.3 Classes
JCHTMLPane
provides all the functionality necessary for an HTML-based pane, whileJCHelpPane
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 asJEditorPane
's. The class behaves like aJEditorPane
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.
Constructors for JCHelpPane
JCHelpPane
's constructors let you specify source pages using URLs orString
s. The latter may be advantageous if you generate some HTML-formatted text dynamically.
8.5.2 Methods
JCHTMLPane
The method of note is
setText()
, which is inherited fromJEditorPane
. Use it to pass text with embedded HTML tabs to theJCHTMLPane
. 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:
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
String myHTMLText = "<HTML><HEAD><TITLE>JCHTMLPane Demo</TITLE></HEAD>";JCHTMLPane
. The result is shown in the accompanying figure.
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
import com.klg.jclass.util.swing.*;JCHelpPane
s with both Strings and URLs. If any of the URLs can't be found, the version ofJCHelpPane
that uses Strings is displayed.
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.
![]() ![]() ![]() |