Adding a Help Menu Item

General Remark

The easiest way to integrate a helpset is via JHelpAction.

Almost always you might want to add a help menu item to your application's menubar to invoke the help viewer. This is just a suggestion, how you could do that.

Code Example

This example assumes, that you have created a helpset Help.hs, that is in the subdirectory helpset relative to the resource directory of your application. It makes sense to make the helpbroker static, because you share it usually between all instances and you only need to create it once.


import javax.help.*;  

import javax.swing.*;  

import java.net.*;  



private static HelpBroker mainHelpBroker = null;

private static CSH.DisplayHelpFromSource csh = null;

private static final String mainHelpSetName = "helpset/Help.hs";

private JMenuItem helpItem = null;

.

.

public void initialize(){



   helpItem = new JMenuItem("Help");

   // add the helpItem to the menubar

   .

   .

   // try to find the helpset and create a HelpBroker object

   if (mainHelpBroker == null){

     HelpSet mainHelpSet = null;

     try {

        URL hsURL = HelpSet.findHelpSet(null, mainHelpSetName);

	if (hsURL == null)

          System.out.println("HelpSet " + mainHelpSetName + " not found.");

	else

          mainHelpSet = new HelpSet(null, hsURL);

     } catch (HelpSetException ee) {

        System.out.println("HelpSet " + mainHelpSetName + " could not be opened.");

        System.out.println(ee.getMessage());  

     }

     if (mainHelpSet != null)

       mainHelpBroker = mainHelpSet.createHelpBroker();

     if (mainHelpBroker != null)

     // CSH.DisplayHelpFromSource is a convenience class to display the helpset

       csh = new CSH.DisplayHelpFromSource(mainHelpBroker);

   }

   // listen to ActionEvents from the helpItem

   if (csh != null)

     helpItem.addActionListener(csh);

   .

   .

   .

}