HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

What Is a Widget?
A widget is a reusable graphical user-interface (GUI) component that operates synergistically with callbacks (a mechanism by which a user's action on a software application's GUI is connected to the code implementing the application's response to this action. The Implementing Callback article last month showed how the callback mechanism can be implemented in Java and how standard AWT components can be extended to support it. Like callback, widget is a familiar concept to X Toolkit and Motif programmers. This follow-up article will introduce the reader to the Widget interface class that helps in the implementation of the widget extension to AWT or other GUI components.

Advantages of Using the Widget Interface Class
The Implementing Callback article showed how the use of the callback mechanism allows the programmer to separate the application's GUI code from the code implementing the application's response to any user interaction with the GUI. To make use of Callbackable and CallbackList classes introduced in that article, GUI component classes have to be widget-ized. However, there's no mechanism in place that requires the widget developer of new GUI components to conform to a standard widget structure. A standard structure has many advantages. In a way, it is a contract between the widget developer and the widget user, in that it guarantees to the widget user the existence of certain widget attributes, such as names. The solution is to use the Java language's interface construct to implement a Widget interface class to enforce the necessary uniformity.

Defining the Widget Interface Class
To support callback, an AWT component where the event originated has to be extended or subclassed to hand off events to the centralized event handler rather than processing the event itself. The programmer supplies the application response code by extending the Callbackable class, and registers this code with the extended AWT component. To simplify the identification of the component that triggers the callback, it is sometimes convenient for the programmer to assign a name to that component that is retrievable at a later time. A Widget should implement at least two methods:

public abstract void addCallback(
String callbackName,
Callbackable callback );
public abstract String getName();

Listing 1 shows the actual implementation of the Widget interface. Widget naming is implemented as a protected variable and can be assigned as a parameter in the constructor. This can be partially enforced only by adding a constructor with just the name parameter, which in essence calls the default constructor and saves the widget name. Implementing the Widget Interface in AWT Button Listings 2 and 3 show the enhanced Button classes for Java 1.0 and 1.1, respectively. Note that these classes differ from the version presented in the Implementing Callback article only by requiring them to implement the Widget interface. If the programmer inadvertently omits support for widget name or callback registration, the Java compiler flags these errors.

The MonthlyCalendar Class and Building Composite Widgets
Using the Widget interface and widget-ized AWT and other GUI components, it's very easy to build a suite of more complex widgets. As an example, Listing 4 shows the MonthlyCalendar class, which displays a one-month calendar of the specified month and year. Listing 5 shows the MonthlyCalendarTest program. Figure 1 shows how the MonthlyCalendarTest program looks when run. MonthlyCalendar is implemented as a Widget. Note its addCallback method, which essentially passes any registered callback down to the Buttons representing the days of the month. Note also that because event handling is delegated to the callback mechanism, MonthlyCalendar is Java version-independent.

Figure 1
Figure 1:

Conclusion
Java's interface language construct makes it easy to enforce uniformity of implementation of widget classes supporting callbacks. By enforcing uniformity, the widget user is guaranteed the existence of certain widget attributes. This, in turn, makes it easy to implement more complex widgets based on standard AWT components. Next month's article will introduce a far more complex widget (a TreeViewer). month. Note also that because event handling is delegated to the callback mechanism,

Download Source Code
Source code for this article can be downloaded from http://www.sys-con.com. A fully implemented version containing the extended AWT code and a WallCalendar class (built on top of MonthlyCalendar) can also be downloaded for a nominal cost from Wigitek Corporation at www.wigitek.com.

About the Author
Daniel Dee has more than 10 years' experience in the development of GUI software toolkits, using X Windows (versions 10 and 11) and Java. He is currently the president of Wigitek Corporation, a software development and consulting firm. He has an MSEE from the University of the Philippines and an MS in computer system engineering from the University of Massachusetts.

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.