Help screens are a necessity for making any application user-friendly. This article will show how the JEditorPane and JFrame classes, along with HTML files, can be used to create help windows for any Java application. This is a project-oriented article that will walk the reader through all the code needed for development. Some familiarity with Java is assumed.
Find below a complete listing of the code in our class. Have a quick look at it now, but don’t worry if you don’t understand all the details. Some basic knowledge of Java is assumed so not all the lines of code will be discussed. Relevant sections will be explained in detail.
1:////////////////////////////////////////////////////////////////// 2:/** 3:* This class creates a frame with a JEditorPane for loading HTML 4:* help files 5:*/ 6://package goes here 7:import java.io.*; 8:import javax.swing.event.*; 9:import javax.swing.*; 10:import java.net.*; 11:import java.awt.event.*; 12:import java.awt.*; 13: 14:public class HelpWindow extends JFrame implements ActionListener{ 15: private final int WIDTH = 600; 16: private final int HEIGHT = 400; 17: private JEditorPane editorpane; 18: private URL helpURL; 19:////////////////////////////////////////////////////////////////// 20:/** 21: * HelpWindow constructor 22: * @param String and URL 23: */ 24:public HelpWindow(String title, URL hlpURL) { 25: super(title); 26: helpURL = hlpURL; 27: editorpane = new JEditorPane(); 28: editorpane.setEditable(false); 29: try { 30: editorpane.setPage(helpURL); 31: } catch (Exception ex) { 32: ex.printStackTrace(); 33: } 34: //anonymous inner listener 35: editorpane.addHyperlinkListener(new HyperlinkListener() { 36: public void hyperlinkUpdate(HyperlinkEvent ev) { 37: try { 38: if (ev.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { 39: editorpane.setPage(ev.getURL()); 40: } 41: } catch (IOException ex) { 42: //put message in window 43: ex.printStackTrace(); 44: } 45: } 46: }); 47: getContentPane().add(new JScrollPane(editorpane)); 48: addButtons(); 49: // no need for listener just dispose 50: setDefaultCloseOperation(DISPOSE_ON_CLOSE); 51: // dynamically set location 52: calculateLocation(); 53: setVisible(true); 54: // end constructor 55:} 56:/** 57: * An Actionlistener so must implement this method 58: * 59: */ 60:public void actionPerformed(ActionEvent e) { 61: String strAction = e.getActionCommand(); 62: URL tempURL; 63: try { 64: if (strAction == "Contents") { 65: tempURL = editorpane.getPage(); 66: editorpane.setPage(helpURL); 67: } 68: if (strAction == "Close") { 69: // more portable if delegated 70: processWindowEvent(new WindowEvent(this, 71: WindowEvent.WINDOW_CLOSING)); 72: } 73: } catch (IOException ex) { 74: ex.printStackTrace(); 75: } 76:} 77:/** 78: * add buttons at the south 79: */ 80:private void addButtons() { 81: JButton btncontents = new JButton("Contents"); 82: btncontents.addActionListener(this); 83: JButton btnclose = new JButton("Close"); 84: btnclose.addActionListener(this); 85: //put into JPanel 86: JPanel panebuttons = new JPanel(); 87: panebuttons.add(btncontents); 88: panebuttons.add(btnclose); 89: //add panel south 90: getContentPane().add(panebuttons, BorderLayout.SOUTH); 91:} 92:/** 93: * locate in middle of screen 94: */ 95:private void calculateLocation() { 96: Dimension screendim = Toolkit.getDefaultToolkit().getScreenSize(); 97: setSize(new Dimension(WIDTH, HEIGHT)); 98: int locationx = (screendim.width - WIDTH) / 2; 99: int locationy = (screendim.height - HEIGHT) / 2; 100: setLocation(locationx, locationy); 101:} 102:public static void main(String [] args){ 103: URL index = ClassLoader.getSystemResource("index.html"); 104: new HelpWindow("Test", index); 105: 106:} 107:}//end HelpWindow class 108:////////////////////////////////////////////////////////////////