Java Reference
In-Depth Information
The source for the custom border and the sample program is shown in Listing 7-3.
Listing 7-3. Custom Colorized Border
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
public class RedGreenBorder extends AbstractBorder {
public boolean isBorderOpaque() {
return true;
}
public Insets getBorderInsets(Component c) {
return new Insets(3, 3, 3, 3);
}
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
Insets insets = getBorderInsets(c);
Color horizontalColor;
Color verticalColor;
if (c.isEnabled()) {
boolean pressed = false;
if (c instanceof AbstractButton) {
ButtonModel model = ((AbstractButton)c).getModel();
pressed = model.isPressed();
}
if (pressed) {
horizontalColor = Color.RED;
verticalColor = Color.GREEN;
} else {
horizontalColor = Color.GREEN;
verticalColor = Color.RED;
}
} else {
horizontalColor = Color.LIGHT_GRAY;
verticalColor = Color.LIGHT_GRAY;
}
g.setColor(horizontalColor);
g.translate(x, y);
// Top
g.fillRect(0, 0, width, insets.top);
// Bottom
g.fillRect(0, height-insets.bottom, width, insets.bottom);
Search WWH ::




Custom Search