Java Reference
In-Depth Information
functions around each other, you will develop a rich set of functions that you can compose at
runtime.
Decorating without Decorator
Having seen some good examples of D ECORATOR , it is important to note that similar designs
allow for "decorating" an object without applying the D ECORATOR pattern. Two areas that
can be mistaken for D ECORATOR are the use of Swing borders and listeners.
A GUI framework developer might apply D ECORATOR in establishing how GUI components
get borders. To do so, you can create a hierarchy of components that expect another
component in their constructor and distribute a paint() method across this hierarchy.
Border classes in this hierarchy would paint their border and then forward the paint() call
to the component they wrap. Swing, however, does not work this way.
Swing anticipates that any component might need a border, and so the JComponent class
includes a setBorder() method. If you want to "decorate" a component with more than
one border, you can apply the CompoundBorder class. The following code gives
an example with results shown in Figure 27.8.
Figure 27.8. Borders can make a component more appealing.
package com.oozinoz.applications;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import com.oozinoz.ui.*;
public class ShowBorders
{
protected static JButton createButton(String label)
{
JButton b = new JButton(label);
b.setFont(SwingFacade.getStandardFont());
b.setFocusPainted(false);
return b;
}
public static void main(String[] args)
{
JButton ignite = createButton("Ignite");
JButton launch = createButton("Launch");
Search WWH ::




Custom Search