advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Download the Code for this Article
Partners & Affiliates
advertisement
advertisement
advertisement
advertisement
Average Rating: 4.4/5 | Rate this item | 27 users have rated this item.
Jazz Up Your JTables with Reusable Classes (cont'd)
3-D Border Affects
You can simulate 3-D affects by manipulating the border of a cell. Create a FlashBorderRenderer, which—like the FlashColorRenderer—uses 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; }
advertisement

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.

Previous Page: Row and Cell Flashing  


Lara D'Abreo is an independent consultant responsible for many commercially released Java products in the telco, database, and integration spaces. She is currently working on software to monitor application servers. to e-mail Lara.
Page 1: IntroductionPage 4: Row and Cell Flashing
Page 2: Custom Rendering—Row Colors and HighlightsPage 5: 3-D Border Affects
Page 3: Separating Renderers from the Application 
Please rate this item (5=best)
 1  2  3  4  5
advertisement