Monday, December 24, 2012

How to add Help menu to a Swing application



A good tester, don’t suppose customers to ramble for needy  material with application in hand. A simple help file should do the needful. Let’s jump to cram about Help file in Swing. JavaHelp is an Open Source help system. To Download JavaHelp, visit http://javahelp.java.net/. I have used Eclipse IDE for this snippet. Configuring JavaHelp in Eclipse and Creating a HelpSet will be covered in future posts.

Prerequisite
·         JavaHelp should be Downloaded.
·         HelpSet(.hs) should be available.
·         Path of HelpSet(.hs) should be configured in your current class path.



import java.awt.event.KeyEvent;

import javax.help.CSH;
import javax.help.HelpBroker;
import javax.help.HelpSet;
import javax.help.HelpSetException;

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


public class SwingAppwithhelp
{
                JFrame appFrame;              
                JMenuBar bar;
                JMenu helpMenu ;
                JMenuItem helpMenuItem;
                HelpSet helpset;
        
               
   public static void main(String args[]){
      new SwingAppwithhelp();
   }

   public SwingAppwithhelp()
   {
                    createGUI();     
   }

                public void createGUI(){

                               
                                appFrame = new JFrame("Swing App with Help");
                                bar=new JMenuBar();        
                               
                                //Arrange components on contentPane and set Action Listeners to each component
                                arrangeComponents();
                               
                                appFrame.setSize(240,300);
                                appFrame.setResizable(false);
                                appFrame.setVisible(true);
                                appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                }
               
                public void arrangeComponents(){
                               
                               
                      // Move help menu to right side           
                    bar.add(Box.createHorizontalGlue());
                    helpMenu = new JMenu("Help");
                    helpMenu.setMnemonic(KeyEvent.VK_H);
                    helpMenuItem = new JMenuItem("Launch Help");
                    helpMenu.add(helpMenuItem);
                    bar.add(helpMenu);
                    appFrame.setJMenuBar(bar); 

                               
                                ClassLoader loader = null;
                    java.net.URL url = HelpSet.findHelpSet(loader, "HelpSet.hs");
                    System.out.println(url.getPath());
                    try {
         helpset = new HelpSet(loader, url);
       } catch (HelpSetException e) {
         System.err.println("Error loading");
         return;
       }
                    
                    HelpBroker helpbroker = helpset.createHelpBroker();                                                          
                               
                                helpMenuItem.addActionListener(new CSH.DisplayHelpFromSource(helpbroker));

                }   

   }

So this how your app should look like, if you followed the above code.Signing off, with this quote "We can't help everyone, but everyone can help someone"


Click Launch help menu item, to invoke Help file

No comments:

Post a Comment