3-D Border Affects
You can simulate 3-D affects by manipulating the border of a cell. Create a
FlashBorderRenderer, whichlike the
FlashColorRendereruses the
FlashProvider interface to determine the flash state of a cell. Inside the
getTableCellRendererComponent() method, set the Border of the cell to raised to give it a 3-D effect:
public class FlashBorderRenderer {
protected static final Border raised = BorderFactory.createRaisedBevelBorder();
protected static final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
protected static final Border focusBorder =
UIManager.getBorder("Table.focusCellHighlightBorder");
...
public Component getTableCellRendererComponent(...) {
// Get the component from the delegate
Component c =
delegate.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
// Convert view column to model column
int mcol = table.convertColumnIndexToModel(column);
// invert the colours to flash
JComponent jc = (JComponent) c;
// If an abstract button e.g JCheckBox then set
// border painted to true
if (jc instanceof AbstractButton) {
((AbstractButton) jc).setBorderPainted(true);
}
if (provider.isFlashOn(row, mcol)) {
jc.setBorder(raised);
} else {
if (hasFocus)
jc.setBorder(focusBorder);
else
jc.setBorder(noFocusBorder);
}
return c;
}
Remember to register the BorderFlashRenderer inside the registerRendererForClass() method to add those 3-D flashing effects to the existing behavior.
Reusable Classes for All JTable Display Effects
Using the techniques outlined in this article, you built a few common classes that you can re-use for all your JTable display effects. With the Decorator pattern, you can dynamically bind renderers together to create complex combinations of user interface behavior. Applying the Provider interface decouples the renderers from the application and makes them portable, which means no more hard coding! So have fun adding color, border, and font effects to your JTables.