Java Reference
In-Depth Information
If you don't want to sidestep the “native” requirement of the Windows look and feel, you
can install individual UI delegates, such as the following, which uses the Windows UI delegate
for the
JButton
component:
UIManager.put("ButtonUI","com.sun.java.swing.plaf.windows.WindowsButtonUI").
Adding UI Delegates
Creating a new look and feel that has custom UI delegates involves creating a subclass of the
LookAndFeel
class. More likely, you'll create a subclass of
BasicLookAndFeel
or another predefined
look and feel class, and then provide your custom delegates for some of the components.
If you subclass the
BasicLookAndFeel
class, it has a
protected void
initClassDefaults(UIDefaults table)
method to be overridden to install your custom UI
delegates. Just put the delegates in the
UIDefaults
table for the look and feel, instead of in your
program that wants to use the new delegate.
The extension to the
MetalLookAndFeel
in Listing 20-10 adds the previously defined
MyComboBoxUI
delegate (Listing 20-7) as the
ComboBoxUI
delegate for the look and feel. As you
define more customized components, you can add them in a similar way.
Listing 20-10.
MyMetal Look and Feel
import javax.swing.UIDefaults;
import javax.swing.plaf.metal.MetalLookAndFeel;
public class MyMetal extends MetalLookAndFeel {
public String getID() {
return "MyMetal";
}
public String getName() {
return "MyMetal Look and Feel";
}
public String getDescription() {
return "The MyMetal Look and Feel";
}
public boolean isNativeLookAndFeel() {
return false;
}
public boolean isSupportedLookAndFeel() {
return true;
}
protected void initClassDefaults(UIDefaults table) {
super.initClassDefaults(table);
table.put("ComboBoxUI", "MyComboBoxUI");
}
}