Java
  Home arrow Java arrow Page 3 - 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 HelpWindow Class
    ( Page 3 of 4 )

    The first thing to notice about our class is that it extends JFrame (line 14). We need not have done things this way. We could simply have included an instance of the JFrame class as a data member. However, we want our class to have all the functionality of the JFrame class. By using inheritance we will be able to directly change the title or the size of our frame by using the parent methods, “setTitle” and “setSize”. After all, this is the whole point of object-oriented languages.

    ActionListener

    Our class will also implement the interface ActionListener (line 14) so that it can easily react to events, principally mouse clicks. This is the most commonly used listener and requires that we implement the “actionPerformed” method.

    Lines 60 through 76 implement this method and process button clicks. This method will be dealt with in detail shortly.

    Data Members & Constructor

    Only four data members are included in our class (Lines 15 through 18). Two are simple integers used to set the size of the frame. These variables are declared as “final” and will be used as default values. Using variables instead of literals makes for easier code maintenance and declaring them “final” means that they cannot be changed.

    The two remaining data members are URL and JEditorPane objects respectively. An URL is fairly self-explanatory, it is constructed from an HTML page in the same directory as our application,  but the JEditorPane is a bit more interesting.

    However, before moving on to a discussion of JEditorPane a few comments about the constructor (lines 24 - 55). are in order. The constructor accepts two arguments, a title for the application and a home page for our help files. The first line of the constructor code (line 25) is a call to the parent constructor in order to set the title of our application. The home page should be a list of hyperlinks to specific help files, although for testing purposes any HTML page will do. This URL is assigned to a class variable.

    JEditorPane Class

    This is the class that will be our help file browser. It can handle content formatted as text, rich text or HTML. We don’t have to do anything special to enable it to handle HTML files. As the Sun online tutorial says, “It effectively morphs into the proper kind of text editor for the kind of content it is given”. This happens on line 30 where the content is set to an URL.

    One important thing that we do need to do with this component is add a HyperTextListener. Lines 35 – 46 create an anonymous inner listener of this class so that information is updated if a hyperlink is clicked. This listener simply calls the “setPage” method to change URLs. If you need more information about listeners see the article  “Listeners In Java” also found on this website.

    The JEditorPane class is a component that gets added to our main, JFrame-derived class. This happens on line 47. Because our parent class is a JFrame it comes with a BorderLayout as its default layout manager. When an item is added to a JFrame’s contentpane its default location will be at the centre of the JFrame and this is exactly what we want.

    The actionPerformed Method

    Buttons are added to our application in the addButtons method (starting on line 80),  and the application itself is added as an ActionListener to both buttons. All this means is that our buttons are able to react to events. The code that processes events is the “actionPerformed” method (line 60 and following).

    Our “Contents” button functions as a Home Page button by returning to the list of hyperlinks to specific help files.

    The “Close” button simply disposes of our help window but perhaps a few comments are in order. The line:

    processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));,

    could just as easily have been replaced with,

    dispose();

    However, creating a window closing event makes for more robust code. If we decide to add a window listener that handles the window closing event then we will ensure that the same code will execute regardless of whether the user shuts down the application from the title bar or by pressing the “Close” button. In other words we will handle the closing event in one location only.

    It is also worth noting that our help window is disposed of when it is closed. This behaviour is set on line 50. It is important that the default close operation not be set to “EXIT_ON_CLOSE” because the class described here is an ancillary class and closing it should not end the application. Also, with this in mind, don’t forget to remove the “main” method before you incorporate this class into another class. The “main” method is included here simply for testing purposes.



     
     
    >>> 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek