Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap