The JavaHelp system provides classes and methods that help you implement context-sensitive help in your applications. The following sections:
The following table summarizes the context-sensitive help system.
CSH Type | Activation Mechanism | Object for Which Help Is Provided | Implementation Steps |
---|---|---|---|
Window-Level | Press F1 (or Help) key | Window with focus |
|
Field-Level |
1. Activate field-level help
2. Navigate with mouse or keyboard 3. Select object |
Selected object |
|
Help Button
Menu Item |
Click button or choose menu item | Topic associated with button or menu item |
|
System Initiated | Internal, varies | Internal, varies |
|
This section describes the low-level elements used in implementing context-sensitive help.
If you use the "convenience" methods in the HelpBroker object to implement
context-sensitive help, these low-level elements are
managed for you.
The basic steps for implementing context-sensitive help are:
In order to provide context-sensitive help for GUI Components and menu items, you must associate a help ID with that Component or menu item. To make that association, you set the helpID property and (if you use multiple HelpSets) the HelpSet for the Component or menu item. The JavaHelp system CSH class provides the following convenient methods for setting and getting the helpID for Components and menu items:
public static void setHelpIDString(Component comp, String helpID);
Sets the helpID for a component.
public static String getHelpIDString(Component comp);
Returns the helpID for a component
public static void setHelpSet(Component comp, HelpSet hs);
Sets the HelpSet for a component.
public static HelpSet getHelpSet(Component comp);
Returns the HelpSet of a component.
public static void setHelpIDString(MenuItem comp, String helpID);
Sets the helpID for a menu item.
public static String getHelpIDString(MenuItem comp);
Returns the helpID for a menu item.
public static void setHelpSet(MenuItem comp, HelpSet hs);
Sets the HelpSet for a menu item.
public static HelpSet getHelpSet(MenuItem comp);
Returns the HelpSet of a menu item.
The context-sensitive help class provides a method you can use to easily track
context-sensitive events. This method traps all non-navigational
events until an object is selected. It returns the selected object.
public static Component CSH.trackCSEvents()
The following sections describe how to use the JavaHelp system to implement the different forms of context-sensitive help.
The JavaHelp system defines a HelpBroker interface that provides "convenience" methods that greatly simplify the job of implementing context-sensitive help. HelpBroker methods hide much of the underlying implementation details--in exchange, using the HelpBroker limits the flexibility of your implementation. For example, if you use the DefaultHelpBroker, you must display help information in the standard help viewer.
You can implement context-sensitive help without using the
HelpBroker, or use it for some tasks and not for others.
For example, if your implementation requires something not
provided in the HelpBroker (for instance, you want to
display context-sensitive help in a different viewer), you
must use the basic classes (CSH, JHelp)
directly. For information about those classes, use
the JavaHelp system apiviewer command.
A HelpBroker's "convenience" methods enable:
A HelpBroker provides the following context-sensitive methods:
public void setHelpSet(HelpSet hs);
Sets the HelpSet for the current HelpBroker (there can be only one HelpSet per HelpBroker). If you use this method to change HelpSets, the displays in the corresponding JHelp component and JHelpNavigator are changed.
public HelpSet getHelpSet();
Gets the current HelpSet for the HelpBroker.
public void setCurrentID(Map.ID id) throws BadIDException;
Sets the current ID that is to be displayed in the help viewer. If hs is null, the HelpBroker's current HelpSet is used. If hs is different than the current HelpSet (and not contained in the current HelpSet), the setHelpSet method is executed.
public void setCurrentURL(URL url, HelpSet hs) throws BadIDException;
Displays the specified URL in the help viewer. If hs is null, the HelpBroker's current HelpSet is used. If hs is different than the current HelpSet (and not contained in the current HelpSet), the setHelpSet method is executed.
public void enableHelpKey(Component comp, String id, HelpSet hs);
Enables the Help key on a Component. This method works best when the component is the rootPane of a JFrame in Swing-based applications, or a java.awt.Window (or subclass thereof) in AWT-based applications. This method sets the default helpID and HelpSet for the Component and registers keyboard actions to trap the "Help" keypress. If the object with the current focus has a helpID, the helpID is displayed when the Help key is pressed, otherwise the default helpID is displayed.
public void enableHelp(Component comp, String id, HelpSet hs);
Enables help activation for a help component (for example, a Help button). This method:
public void enableHelp(MenuItem comp, String id, HelpSet hs)
Enables help activation for a MenuItem. This method:
public void enableHelpOnButton(Component comp, String id, HelpSet hs)
Enables help for a Component. This method sets the helpID and HelpSet for the Component and adds an actionListener. When an action is performed it displays the Component's helpID and HelpSet in the default viewer. If the Component is not a javax.swing.AbstractButton or a java.awt.Button an IllegalArgumentException is thrown.
public void enableHelpOnButton(MenuItem comp, String id, HelpSet hs)
Enables help for a MenuItem. This method sets the helpID and HelpSet for the Component and adds an actionListener. When an action is performed it displays the helpID and HelpSet in the default viewer.
The CSH class contains three inner classes that provide support for context-sensitive help.
CSH.DisplayHelpAfterTracking
An ActionListener that displays help on a selected object after tracking context-sensitive events. Its constructor takes a HelpBroker object.
CSH.DisplayHelpFromFocus
An ActionListener that displays the help of the object that currently has focus. This method is used to enable HelpKey action listening for components other than the RootPane or window. This listener determines if the object with the current focus has a helpID, if it does the helpID is displayed. If the object does not have a helpID, the helpID on the action's source is displayed (if one exists).
CSH.DisplayHelpFromSource
An actionListener that gets the helpID for the action source and displays the helpID in the help viewer. Its constructor takes a HelpBroker object.
Start your window-level help implementation by setting the helpID and (if you use multiple HelpSets) the HelpSet for each component for which you want help. If you do not set help for a given component, CSH.getHelpID() recursively searches through the component's ancestors until it finds the first ancestor with a helpID, or until it reaches the last ancestor. For example:
: JTextArea newText = new JTextArea(); hb.enableHelp(newText, "debug.overview", hs); :
: rootpane = frame.getRootPane(); mainHelpBroker.enableHelpKey(rootpane, "top", null); :
Start your field-level help implementation by setting the helpID and (if you use multiple HelpSets) HelpSet for each component for which you want help. If you do not set help for a given component, CSH.getHelpID() recursively searches through the component's ancestors until it finds the first ancestor with a helpID, or until it reaches the last ancestor.
After you set the helpID/HelpSet for all components, create a field-level help button or menu item. Set an ActionListener on the button or menu item with a HelpBroker object using getOnItemActionActionListener. For example:
JToolBar toolbar=new JToolBar(); CSH.setHelpID(toolbar,"toolbar.main"); : helpbutton= addButton(toolbar, "images/help.gif", "help"); helpbutton.addActionListener(new CSH.DisplayHelpAfterTracking(mainHelpBroker));
To implement Help menu or Help button help:
JButton helpButton = new JButton("Help"); mainHelpBroker.enableHelpButton(helpButton, "browse.strings", null);
Although system initiated help is rarely implemented using the help viewer, it is simple to do. When help is presented internally within an application, pass a valid helpID to a HelpBroker object. For example:
: try { mainHelpBroker.setCurrentID(helpID); } catch (Exception ee) { System.err.println("trouble with visiting id; "+ee); } :
The following example shows the code required for the different types of context-sensitive help using a default HelpSet:
try { ClassLoader cl = ApiDemo.class.getClassLoader(); URL url = HelpSet.findHelpSet(cl, helpsetName); mainHS = new HelpSet(cl, url); } catch (Exception ee) { System.out.println ("Help Set "+helpsetName+" not found"); return; } catch (ExceptionInInitializerError ex) { System.err.println("initialization error:"); ex.getException().printStackTrace(); } mainHB = mainHS.createHelpBroker(); : // Enable window-level help on RootPane rootpane = frame.getRootPane(); mainHB.enableHelpKey(rootpane, "top", null); : // Enable field-level help on various components JToolBar toolbar=new JToolBar(); CSH.setHelpIDString(toolbar,"toolbar.main"); : //Enable Menu/Button help on Help menu item helpbutton= addButton(toolbar, "images/help.gif", "help"); mainHelpBroker.enableHelpButton(helpbutton, "browser.strings", null); sourceIFrame = new JInternalFrame("Source", true, true, true, true); CSH.setHelpIDString(sourceIFrame, "edit.editsource"); JTextArea newtext=new JTextArea(); CSH.setHelpIDString(newtext, "build.build"); newtext = new JTextArea(); CSH.setHelpIDString(newtext, "debug.overview"); newtext = new JTextArea(); CSH.setHelpIDString(newtext, "browse.strings"); : // System Level help somewhere within the code try { mainHelpBroker.setCurrentID(helpID); } catch (Exception ee) { System.err.println("trouble with visiting id; "+ee); } :
See also: