by Al Saganich
In the past couple of issues of Visual J++ Developer's Journal, we've discussed some new features of the SDK 2.0 beta. These features were common to both Visual J++ and the version 1.1.x JDK from Sun. However, Microsoft's SDK 2.0 beta contains another interesting feature not currently provided in the JDK—the Application Foundation Classes (AFC). It wouldn't surprise us if the new AFC classes become part of the next release of Visual J++ and come standard with future releases of Windows 95 and Windows NT.
The AFC contains a number of classes, which are designed to help application developers create interesting and full-featured applications, and go beyond the confines of the simple buttons and menus provided by the base Java JDK. Some of these features are
In this article, we'll introduce the new AFC, from a GUI perspective. We'll also develop robust applications using both the new UI controls and the Event Delegation Model.
We'll begin with the basics of the AFC framework and the AFC component model. The AFC component model is similar to the standard JDK component model. It defines components, their containers, and layout managers to manage the components' positions.
The differences between AFC versions of the JDK classes primarily deal with providing faster lightweight classes that are based on a peerless implementation. The components' containers and layout managers that are used to develop applications all work within the AFC framework.
Please note: The AFC classes, in many instances, don't extend the AWT component class but rather provide replacements. As a result, you can't mix AWT and AFC components within a single application. However, the AFC does provide a set of compatibility classes that allows standard JDK-based applications to use some of the new AFC classes.
The AFC framework is based on several extensions to standard JDK classes. Applications and applets are now derived from either AwtUIFrame or AwtUIApplet. These two classes extend their JDK parents, Frame and Applet, to include functionality for supporting the UI classes provided by the com.ms.ui package.
In addition to AwtUIFrame and AwtUIApplet, the AFC provides a number of other extensions to existing classes, including
Now that we've reviewed the basics, let's look at a skeleton application, which you can run as standalone or from within an HTML page.
All applications begin basically the same way—by deriving a class from AwtUIApplet, then adding functionality to that applet. Our application is no different. Listing A shows a trivial applet that does little more than define a frame and set its color to light gray, as you can see in Figure A. However, our applet does show two important aspects of AFC-based development—the use of AwtUIFrame and AwtUIApplet. (We've highlighted some of the more important features of the listing in bold.) Figure A shows the result of running AFCSkel.java using jview.exe.
Listing A: AFCSkel.java
// AFCSkel.java
import com.ms.ui.*;
import com.ms.fx.*;
import java.awt.*;
import java.awt.event.*;
public class AFCSkel extends AwtUIApplet
{
// Standalone main
public static void main(String args[])
{
// Create a frame to contain applet
MyFrame f = new
MyFrame ("AFC Skeleton");
f.show ();
f.setSize (320, 240);
// Create instance our applet and
// add to frame
AFCSkel applet = new AFCSkel ();
f.add (applet);
// We must explicitly call init()
applet.init ();
}
public void init()
{
setBackground (FxColor.lightGray);
add (new MyUIPanel ());
validate ();
}
}
// An adapter to handle the user exiting
// via the exit (X) system menu item.
class ShutdownAdapter
extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
// Frame extension which uses
// shutdown adapter
class MyFrame extends AwtUIFrame
{
// Constructor
public MyFrame(String str)
{
super (str);
addWindowListener(
new ShutdownAdapter());
}
}
class MyUIPanel extends UIPanel
{
// Constructor
public MyUIPanel ()
{
setBackground (FxColor.lightGray);
}
}
Figure A
This simple AFC-based applet displays a frame and sets its color.
First and foremost, you'll notice two differences between our new AFC-based application and its JDK cousins. Under the JDK, we derive applets from java.awt.Applet; under the AFC, we derive our applets from com.ms.ui.AwtUIApplet. In fact, AwtUIApplet is derived from Applet! The reason for this new class is to support additional class methods that work with the new UI classes. These new methods, a couple of which are shown below, look and act like their AWT cousins:
These two methods work exactly like their AWT cousins. In fact, programmers familiar with the AWT model will need little coaching to jump into AFC-based programming.
As with the AwtUIApplet class, the AwtUIFrame class is the starting point for frame-derived classes. The new frame class is very much like AwtUIApplet, containing methods that support AFC UI-based development. The two methods previously mentioned exist with identical signatures in the AwtUIFrame class. When developing AFC-based applications, you always begin with either AwtUIFrame or AwtUIApplet.
Our skeleton application contains several other interesting points to examine. The most important point is the use of the Event Delegation Model to handle events. Users more comfortable with the old event model will be happy to note that the AFC supports the JDK 1.02 handleEvent and action methods, as well as the new Event Delegation Model.
For more information on the Event Delegation Model, refer to the articles "Using The New JDK 1.1 Event Delegation Model With Visual J++" (Visual J++ Developer's Journal, October 1997) and "Working With Inner Classes" (Visual J++ Developer's Journal, November 1997). Both deal with using Java language features and support classes targeted at event handling.
In our AFCSkel.java example, we used only a single Event Delegation Model method, addWindowListener. This method allows the frame class to respond to window events, such as windowClosing. In "Creating an AFC Application" we examine a broader use of this functionality to handle push button, radio button, and dropdown combo box events.
The final interesting feature of the skeleton application is that it contains a
main method. I always add a main method to my applets because it simplifies testing and allows for easy unit testing. The main method must do the following things to support our applet:
In this article, we've covered the basics of the new AFC. We also used a very simple example to demonstrate the basic structure of an AFC application.
------------------------
Al Saganich is an independent software consultant, currently under contract to Digital Equipment Corp. He is co-author of the best-selling Microsoft Visual J++ 1.1 Sourcebook (J. Wiley and Sons, ISBN 0-471-17850-3) and is currently working on the next release of the Java 1.2 for C/C++ programmers to be published in the fall of 1997.
-----------------------
This article is reproduced from the December 1997 issue of Visual J++ Developer's Journal. Visual J++ Developer's Journal is an independently produced publication of The Cobb Group. No part of this article may be used or reproduced in any fashion (except in brief quotations used in critical articles and reviews) without prior consent of The Cobb Group. To contact The Cobb Group, please call (800) 223-8720 or (502) 493-3200.
Copyright © 1997 The Cobb Group, a division of Ziff-Davis Inc. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis is prohibited.