Java
  Home arrow Java arrow Page 2 - Java Help Files
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
JAVA

Java Help Files
By: Peter Lavin
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 65
    2004-07-21


    Table of Contents:
  • Java Help Files
  • The Code
  • The HelpWindow Class
  • Enhancements

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Java Help Files - The Code
    ( Page 2 of 4 )

    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 Articles          >>> More By Peter Lavin
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek