Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

A Java Skeleton Code Generator


July 1999/A Java Skeleton Code Generator

A Java Skeleton Code Generator

Pat Paternostro

Maybe Java isn't as portable as advertised, but this handy generator makes it more so.


If you program in Java, it is not unusual for you to have multiple versions of the JDK (Java Development Kit) installed on your computer. If you are like me, you probably use certain JDK versions for specific projects. When I am working on a new project I usually start out with the basic "skeleton" code necessary for that specific project. For example, I might be working on a Java GUI application written in JDK 1.1.x that uses Swing components. I might also be working on a combination Java applet and GUI application written in JDK 1.0.2 that uses AWT (Abstract Window Toolkit) components and contains a standard menu. Either way, I find myself writing the same skeleton code or copying existing code from another project. I decided to create a Java GUI application, written in JDK 1.1.x, that generates the minimal skeleton code necessary for the different Java program types. I call it, appropriately enough, Skeleton.

The Skeleton User Interface

I have identified four basic Java program types: a console application, a GUI application, an applet, and a combination of an applet and a GUI application. The last program type is possibly the most desirable and functional of the four, since it will run as an applet and as a GUI application from the same source code. (Note: you must take care not to add methods specific to either the applet or GUI program type; that would break the duality of the code.) I include support for both the AWT and Swing components and all the major versions of the JDK (1.0.2, 1.1.x, and 1.2.x) as well as adding menu support for the GUI program types.

I chose a simple design for the application's user interface. As you can see from Figure 1 the application is pretty intuitive. You select the desired functionality from the provided choices and click on the Generate button to generate the skeleton code.

Code Generation Specifics

A partial listing of the skeleton code generator appears in Figure 2. (The function bodies are not shown. The full source code is available from the CUJ ftp site. See p. 3 for downloading instructions.) The Skeleton application is made up of three classes, Skeleton, SkeletonFrame, and MessageBox, located in Skeleton.java

The Skeleton class contains only one method, main, which is the entry point for all Java applications. The main method simply instantiates the SkeletonFrame class, where the heart of the application resides.

The SkeletonFrame class extends the java.awt.Frame class and implements three event listeners: java.awt.event.ActionListener, java.awt.event.TextListener, and java.awt.event.ItemListener. All the application's components, except for the label components, are declared as instance variables for easier referencing later in the code. There is one instance variable that is worth mentioning. It is the java.lang.String instance variable newLine. This variable contains the string representation of the operating-system-specific line separator, which is used to separate lines of generated code. This instance variable makes the Skeleton application platform-independent; different operating systems (e.g., Unix, MacOS, Win 95/98/NT) represent the line separator differently.

The itemStateChanged method enables and disables components based on selections made to the user interface. For instance, the Add Standard Menu to GUI Program checkbox is enabled only when selecting a GUI program type (GUI or Applet/GUI) or when selecting an Applet using Swing components. Also, the Swing Components radio button is enabled only when selecting either the JDK 1.1.x or JDK 1.2.x radio buttons, since Swing components are not supported in a JDK 1.0.2 applet or GUI program.

The textValueChanged method enables and disables the Generate button based on the length of text in the Java Source File Name field. This prevents the user from clicking on the Generate button without first typing something in for a filename.

Clicking the Generate button causes the generator to check if the filename typed in contains a .java extension. If no .java extension is given, one is appended to the filename. The generator then determines whether the file already exists, and displays a warning dialog box if it does. If the file already exists the application does not progress past this point; otherwise, the file is created and one of the code generating methods is called based on the program type selection made.

The generateConsoleApp method is called when you select the Console program type and click on the Generate button. The generated code for this program type is minimal. An example appears in Figure 3 [1].

The generateApplet method is called when you select the Applet program type and click on the Generate button. If the user has elected to use Swing components for the applet, this method adds a menu bar to the applet if the Add Menu to GUI Program checkbox is checked. Unlike AWT applets, Swing applets support the addition of a menu bar.

The generateAppletGUI method is called when you select the GUI or Applet/GUI program types and click on the Generate button. This method's code determines the JDK version chosen as well as if the Swing Components radio button is selected. The only difference you will notice between choosing JDK 1.1.x using Swing components versus choosing JDK 1.2.x using Swing components is that the import statement is different (com.sun.java.swing.*; vs. javax.swing.*; respectively). The code generated from this method takes into account the Java version selected, ensures that the generated code uses the proper event-handling model, and uses non-deprecated methods. For example, if JDK version 1.0.2 is selected, this method generates hierarchical event model code; otherwise it generates delegation event model code. Also, when viewing the generated code for the Applet/GUI program type you will notice that it determines if an AppletContext exists. This prevents the System.exit method from executing in the context of an applet, which should never be allowed to happen. An example of code generated via this method appears in Figure 4 [1].

The MessageBox class is a helper class that extends the java.awt.Dialog class and is used to display both error and informational messages.

Conclusion

The application presented here can be a great time saver when starting a new Java project. You can extend and enhance the code as changes to the JDK occur or to fit your individual needs.

Note

[1] In Figures 3 and 4, the class names are automatically generated based on the filename typed in by the user.

Pat Paternostro is the Director of Education for Tri-Com Consulting Group, located in Rocky Hill, CT. Tri-Com provides programming services for a wide variety of development tasks. You can reach Pat at [email protected].


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.