How to develop commercial-quality Java programs

By exploring infoBook, a customizable, open Java app, this successful developer shows you how to build your own commercial product

When the folks at Sun released the Java Development Kit (JDK) last year, programmers from all walks of life decided to try their hand at developing applets and applications using the new language. Just visit the Gamelan and Java Applet Rating Service (JARS) sites, and you'll see what I mean. However, many of the applets available on the Web are not necessarily the best models for developing Java applets and applications, nor do they take full advantage of the Java language.

In this article I'll provide some guidelines for developing commercial-quality applets, along with some tips on how to package and market your Java applet or application. I use my applet infoBook as an example -- mainly because it has an open architecture. It can be used as a product catalog, phone book, real estate lister, or whatever else you can think of. Additionally, it can be connected to any data source such as RDBMS, X.500, or flat files.

Before you begin coding, it is best to spend some time designing your application, as a well-designed application will most likely result in a product with a solid architecture once it has been built.

Applets versus applications

One key point for consideration is whether you want to develop an applet, which runs inside a Web browser, a standalone application, which can be invoked similar to Microsoft Windows and X-Window programs (for example, Notepad and Xterm, respectively), or both. It is actually easy to develop what I call an appletation -- something that can run as an applet or as a standalone application. InfoBook is designed to run either way. To achieve this flexibility, a bit of custom coding is required for each mode -- applet or application -- due to the particular capabilities and restrictions in both.

For security reasons, applets cannot read or write from or to a user's local disk. Applications cannot perform tasks such as playing audio files or linking to URLs in a Web browser but can access a local disk, execute local programs, and much more. To adapt to the modes of either applet or application, you simply have to use some sort of flag to indicate either the applet or application mode your appletation is running in. This flag -- say a boolean variable named inAnApplet -- can be set to a default value of true. If the appletation is invoked as a standalone application, then this flag can be set to false in the main() method. From there on, before executing some applet- or application-specific code, you can check this flag to determine what to do.

Listing 1 shows an example of how infoBook uses the flag concept to access its data from a local disk using the FileInputStream class or from a remote host using the URL classes.

DataInputStream dis=null;
if (!inAnApplet)
    FileInputStream fis=new FileInputStream(dataSource);
    dis=new DataInputStream(fis);
{ urlDataSource=new URL(applet.getCodeBase(),
    dis=new DataInputStream(urlDataSource.openStream());
Listing 1: Applet versus application logic flow

Applets: Embedded versus framed

There are two modes for invoking applets. The first one, which is more common, is what I call an embedded applet (shown here) as it becomes part of an HTML page.

infoBook for Non-Java Browsers
Sorry, you are not running a Java enabled browser. If you were, you would be able to use the applet shown in the following image:

The second one, located at the URL listed below, is a framed version or what I call a pop-out applet, as it literally pops out of the browser and runs in its own frame.

The framed applet is not too hard to develop but does require some additional coding, mainly to provide a frame for the applet itself. This generally can be done by using another class (for example, InfoBookFrame.class; See Listing 2), which inherits from the Frame class and handles only one event in its event handler -- to dispose of the frame when the user decides to quit the applet. The remaining events are handled by the main applet class (for example, infoBook.class). The key benefit of a framed applet is that it presents a professional look and feel.

public class InfoBookFrame extends Frame { infoBook applet; . .

public InfoBookFrame(infoBook ib, String title, int w, int h) { super(title); . .

MenuBar mb = new MenuBar(); mb.add(fm); setMenuBar(mb);

setLayout(new BorderLayout(1, 2)); add("Center", applet); . . }

public boolean handleEvent(Event evt) { if ( == Event.WINDOW_DESTROY || instanceof MenuItem) { applet.destroyFrame(); if (applet.inAnApplet == false) System.exit(0);

return true; }

return false; } . . }

Listing 2: Providing a Frame for an applet

Properties versus HTML parameters

Parameters are a nice way of providing customization options to users of your application. There are a couple ways to obtain parameters in Java:

  • For applets, most developers use the getParameter() method and <PARAM> tags.

  • For applications, command-line arguments are generally used.

But, there is a third way that can accommodate both cases: Use the java.util.Properties class. The java.util.Properties class inherits from the java.util.Hashtable class and uses keyword=value pairs on each line to store and retrieve parameters. You can use a file similar to Windows INI files to store parameters in, as shown here:

#    File: infobook.cfg
#    Date: Sep, 1996
# Purpose: Provide properties/parameters for infoBook
HelperApp.avi=c:\\windows\\mplayer.exe %i

InfoBook uses such a parameter file in which to store its parameters. Additionally, it gives the user the choice of using HTML parameters. If HTML parameters are used, then infoBook builds a Properties object from the HTML parameters by using the Applet.getParameter() and Properties.put() methods. (See Listing 3.) Otherwise, the Properties object is built by reading in a property file using an input stream. (See Listing 4.)

String validParms[] = { "AutoWrapTextAreas", "CfgFile", "DataSourceFlags", . . . "VisibleListRows" };

String paramValue=null; for (int i=0; i < validParms.length; i++) { paramValue=applet.getParameter(validParms[i]); if (paramValue != null) properties.put(validParms[i], paramValue); }

Listing 3: Loading a Properties object from HTML parameters
if (inAnApplet)
    URL url = new URL(getCodeBase(), propertyFile);
    properties.load(new FileInputStream(propertyFile));
Listing 4: Loading a Properties object from an input stream

In both of the above cases, HTML parameters and input stream, infoBook uses a Properties object internally to pass parameters to various methods. In fact, one of the key benefits of infoBook is its ability to connect to any data source through its plug-in classes. Using a Properties object ensures that, regardless of how parameters are passed in to infoBook, the plug-in classes always have access to those parameters in a consistent fashion.

A customizable user interface

You can design your appletation's user interface with a great deal of flexibility to adapt to different user requirements. For example, infoBook provides the following customization options:

  • Ability to run as an embedded or framed applet

  • Resize flexibility for the applet, the picture in its multimedia panel, and the list box

  • Ability to change the meaning of fields by modifying their values and labels (see Figure 1)

  • Ability to change the label on certain buttons

  • Ability to alter the group heading
infoBook's Data Fields
Figure 1: infoBook's data fields

Again, much of the flexibility mentioned above is achieved through the use of parameters; infoBook provides more than 35 parameters, many of which apply to the user interface. Additionally, Java's layout managers are handy tools that can automatically resize and reshape GUI components, allowing users to alter to a great degree the size of an applet or application. InfoBook only uses two layout managers -- GridBagLayout and GridLayout. GridBagLayout is used for most of infoBook's GUI except for infoBook's buttons, which are aligned using GridLayout.

Replaceable class files

A key benefit of Java is its dynamic binding of objects. This concept is similar to that of dynamic link libraries (DLLs) on Windows, or shared libraries on Unix, except in Java it is greatly simplified. Dynamic binding provides the ability to develop generic classes that can be substituted by developers at a customer site if the proper API specifications are provided.

InfoBook's plug-in classes are based on this concept. For example, one of the three classes that can be replaced or substituted by customers is InfoBookRecSet.class. (See Figure 2.)

infoBook's plug-in class files
Figure 2: infoBook's "plug-in" class files

The InfoBookRecSet class is instantiated by infoBook when a user selects the Open or Search button, as shown here:

InfoBookRecSet recSet=new InfoBookRecSet();

Thanks to dynamic binding, customers can provide their own implementation of InfoBookRecSet.class, which can extract data from a relational database, flat files, X.500, or another data source. Note that the infoBook software comes bundled with a default implementation of InfoBookRecSet.class, which loads data from field-delimited flat files.

Multimedia, threads, browser control, and so on

Multimedia support is almost essential in any applet or application with a GUI. While some of the multimedia features -- such as playing audio files -- are only available when the program is running as an applet, standalone applications can use other helper applications to provide multimedia support. In fact, infoBook uses this concept extensively to support a potentially unlimited list of multimedia formats by allowing users to associate file extensions with their corresponding helper applications. For example, an AVI file can be associated with the Windows Media Player utility in infoBook.

For displaying images, infoBook uses the getImage() method in the java.awt.Toolkit class, instead of java.applet.Applet, since the first one works in both applet and application modes. InfoBook also provides a ticker tape feature, which is developed using methods such as drawRect(), setColor(), drawString(), and others.

Threads can make an applet, application, or appletation very robust, as they provide the ability to handle multiple tasks simultaneously. InfoBook uses several threads to operate itself -- such as threads for ticker tape, image loading, audio loading, and the applet itself.

Browser control includes the ability to send e-mail, make hyperlinks, and use different target browser windows. Sending e-mail from within a browser is achieved by constructing a object with the mailto:emailid@domain (for example, Hyperlinking is handled via the showDocument() method, which comes in two flavors: One instructs the browser to display a URL in the current window, and the other can be used to send the output of a URL to another target browser window.

Some other features to consider for your Java programs is CGI scripting, the ability to extend Java programs via Javascript, and providing font and color control for them. InfoBook provides all these features. The flexibilty of extending Java programs via CGI scripts and Javascript is specially valuable. For example, infoBook provides a button that can be used to invoke a CGI script to which the fields shown on infoBook's screen are passed. InfoBook also allows its data to be accessed from Javascripts. This flexibility of extending infoBook via CGI scripts and Javascript opens up a lot of possibilities for other applications -- such as a shopping-cart system that can use infoBook as a product catalog but utilitize JavaScript to interact with HTML forms and/or CGI scripts.


In this article I have provided a few guidelines for developing good Java applets, applications, or appletations. I have also offered simple packaging and marketing tips that should work well for most applications. To see some of the infoBook examples used in this article, visit the infoBook site listed below in the Resources section. Best of luck!

Anil Hemrajaniis the founder of Chipsoft. His company provides software engineering, consulting and training services to Fortune 500 companies. His home page is located at:

Learn more about this topic