|
Custom Groups and Widgets
A typical example of customization is to subclass either CommandGroup or ToggleCommandGroup to provide specialized widgets. The following example is taken from the webstart demo, and shows two key methods of PageSelectorGroup that create the button bar on the left of the demo window. Creating the widget
/**
* Creates a new JButtonBar with the specified orientation using the specified face.
* @param orientation the orientation of the bar, either {@link JButtonBar#VERTICAL} or
* {@link JButtonBar#HORIZONTAL}.
* @param faceName the face for button bar and its members to use.
*
* @return a new JButtonBar for this group.
*/
public JButtonBar createButtonBar(int orientation, String faceName)
{
JButtonBar buttonBar = new JButtonBar(orientation);
bindMembers(buttonBar, buttonBar, getButtonFactory(), faceName);
return buttonBar;
}
The most important line in the method is the call to bindMembers. This method binds the group to an instance of java.awt.Container and ensures that the container is kept in sync with the group's members. There are two flavours of this method, one ensures the container is populated with buttons, the other with menu items. There are four arguments to the method, which are as follows.
Points worth noting:
Adding a custom face nameWhile there are no special requirements for adding a custom face name, there are a couple of things that make the behaviour more consistent for other developers. These are as follows:
When overriding getAlternativeFaceNames you should always invoke the super class for face names you don't care about. In the above example, the method ensures the "page-selector" face will default first to the button and then to the default face.
/**
* Overrides the default implementation to provide defaults for the page-selector face if
* it hasn't been specified.
* @param face the desired face name
* @return a String array with the alternate faces in preferred order.
*/
public String[]
getAlternativeFaceNames(String face)
{
if (PAGE_SELECTOR_FACE.equals(face))
return new String[] {Face.BUTTON, Face.DEFAULT};
return super.getAlternativeFaceNames(face);
}
|