Home arrow Java & J2EE arrow Page 2 - Java Help Files

The Code - Java

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.

TABLE OF CONTENTS:
  1. Java Help Files
  2. The Code
  3. The HelpWindow Class
  4. Enhancements
By: Peter Lavin
Rating: starstarstarstarstar / 67
July 21, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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:////////////////////////////////////////////////////////////////




 
 
>>> More Java & J2EE Articles          >>> More By Peter Lavin
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: