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

Learn how a simple design pattern can make sophisticated JTable effects easy. Add color, style, and pizzazz, and make your rows and columns automatically highlight, change color, and flash in response to real-time data updates. 


advertisement
ome time ago, I was faced with a tricky user interface problem: develop a suite of real-time Swing stock displays that flash and color themselves according to the data on the screen. It didn't seem too hard—except that my users wanted total control. They each wanted to select the row colors and cell fonts themselves. Some wanted pink, while others wanted blue.


After a bit of research, I couldn't find any well-known method for doing this with JTable components. Sure, I could have written lots of custom rendering code, but I had more than 20 displays to implement and not much time. I needed a simple, flexible approach that I could reuse for every display.

I eventually discovered a solution: the Decorator (GOF) design pattern. The Decorator pattern allows components to be arbitrarily combined or enclosed. Think of it as a Russian Doll technique. Components are wrapped one inside the other so that they can work together for a particular task or purpose. By using the Decorator pattern, I can create a few simple custom renderers and then combine them in various ways to achieve different effects. In addition, by laying down a set of well-defined interfaces to encapsulate my display data I can have all the building blocks I need.

JTable Rendering
Rendering is the mechanism by which Swing paints user interface components on the screen. In JTables, table cell renderers control rendering. They are responsible for drawing individual cells. They control the formatting of the data in the cell: the font, border, and background and foreground colors. Renderers are shared across cells. Think of them as rubber-stamps (Swing tutorial) that move from cell to cell, formatting and displaying data from the table model.

Table cell renderers may be attached to columns or to Java types. Swing provides a rudimentary set of pre-built renderers, which support the simple formatting and display of such Java types as Boolean, Image, ImageIcon, Date, Double, Float, Number, and Object. You don't have to do anything special to obtain these renderers; you get them for free with every JTable.





How do I develop a suite of real-time JTable displays that automatically flash and color themselves according to data updates on the screen without writing a lot of custom rendering code?



Use the Decorator pattern to create custom renderers and then combine them in various ways to achieve different effects. Acquire all the building blocks you need by laying down a set of well-defined interfaces to encapsulate your display data.

  Next Page: Custom Rendering—Row Colors and Highlights


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