Contents | Prev | Next

AWT Additions

Two new attributes have been added to java.awt.Component to aid in the internationalization of a program's GUI.

Name Attribute

The Name attribute is a String which serves as a non-localizable identifier for a Component object. New constructors have been added for Component and its subclasses to allow the name to be set. If these constructors are not used, Components will be given a default name. The method Component.getName() can be used to examine a Component's name.

The Name attribute is particularly useful in writing Action handling routines in which a reference to the target is not known ahead of time. Such Action handlers are often generated by GUI builders. Previously, these routines tried to identify the target Component by looking at its label string. This approach fails when the label strings are localized. The following example shows the Name attribute used by an Action handler.

public boolean action(Event event, Object arg) {
String targetName = event.target.getName();
if( targetName.equals("OKButton") ) {
// Handle OK button press.
return true;
} else if( targetName.equals("CancelButton") ) {
// Handle Cancel button press.
return true;
} else {
return false;
}
}

Locale Attribute

A Locale attribute has been given to the class Component. The methods getLocale() and setLocale() have been added to Component to access this attribute. If a Component's Locale is not explicitly set, its value defaults to the Locale of the Component's parent. If no Component in the hierarchy has an explicit Locale, the default is the value of Locale.getDefault().

The Locale attribute of Component allows the GUI (or portions of the GUI) to maintain its own default locale. This would be useful, for example, for an applet that used the Japanese locale even when the rest of the browser was using the USA locale.



Contents | Prev | Next
java-intl@java.sun.com
Copyright © 1996 Sun Microsystems, Inc. All rights reserved.