<$BlogRSDUrl$>

Fuzzy Space

Two Confused Minds, One Eye-Opener.

Friday, April 14, 2006

HOWTO: Java Swing Custom TableCellRenderer.

Great apologies to everyone (or anyone at all ;op) who had long awaited for my first tech entry. More importantly, apologies to my co-contributor of this blog. She has done much more for this blog than me! Thanks! ;o> The first entry is always difficult. I am not sure whether the topic is too basic. However, I will try to keep my entries short and simple, as I think whoever finds it useful like the important points to be clearly presented. The first topic I am going to cover is a common Swing component, JTable. Typically, JTables are used to display data. The Sun Java Tutorial is a great source of information on how to accomplish some common table-related tasks. One of the key feature of JTable is the default cell renderer. There is an automatic mapping between the column class type and the predefined cell renderer used. For example, a Boolean class type data will be displayed as a JCheckBox, which is more intuitive than displaying the words "true" or "false". All you need is to do is to override the getColumnClass in your TableModel class. The code snip below shows an example of the overridden class.
Below is a simple table from the Java Tutorial that is implemented using the default renderer. The Boolean "vegetarian" column is rendered as a JCheckBox. However, if you noticed, the columns are all of same size and text aligned to the left.
In order to fix this problem, you'll need to use a custom table cell renderer in which you can specify the horizontal alignment. You'll probably use the DefaultTableCellRenderer to do the job. The following code snip shows how,
Be sure to set the default renderer to your custom renderer or turn off the auto-create-column option. Otherwise, you'll have twice the number of columns, one by the default renderer and one by your custom renderer.
This works fine for String columns, but you'll realise the predefined renderers are not used. A Boolean column will be displayed as "true" or "false", instead of a JCheckBox. This is because the "default cell renderer" is not "DefaultTableCellRenderer". Therefore you'll actually need to write your own custom renderer to display the cells the way you want. The trick to writing a custom renderer class is to either extend from the DefaultTableCellRenderer or extend the component class you want to render and implement the TableCellRenderer class, then override the getTableCellRendererComponent method. Using the same method, you can also define color to render the cell.
Below are some useful resources you need to construct your perfect JTable, IBM Developer Article: Rendering cells in Swing's JTable component Javadoc: JTable Java Tutorial: How to Use Tables Sample Code from CSUN: TableDemo with Custom TableCellRenderers Hope that you find this entry useful! :o>

Saturday, March 18, 2006

A Brand New Skin.

I have not got time to post my first technical entry yet. Did some quick customization using a blog skin from Blogger-Template, and made some tinny-winny changes. Although I am not very satisfied with it (there's no room for links, profile and stuffs), but it'll stay for awhile till I find a better one. Anyway I like the theme so far - "An eye-opener for both of us as who seek for knowledge to fill our potholes of ignorance". Just in case I decided to change the skin, below is a picture of how it looks like at the time of this post.


Thursday, March 09, 2006

Let the Ball Roll.

First of all, before I start a quick entry of the background (my side of the story), I want to thank my girlfriend, carine~swan, for starting this little space. In fact, after reading several tech blogs in the past few months, I had wanted to start one for my project team, in which we can share the lil' tips we pick up during the course of our work. However, the official idea was put down as my manager thought it wasn't the right time for such a blog yet (we have occasional sharing sessions). I thought why not just deploy one on my own machine, get some interest and let the idea grow among my colleagues. Alas, I was either too busy or lazy (???) and procastinated. So it is great to start off with this space now! Despite focusing on different aspects of technology in our jobs, I feel that we still speak the same language, the CE-Language, the hardware-software-combo. =p And now, for my side of the story... basically what I will be posting in this space little tips and knowledge that I want to share. I am no expert, so there will be nothing in depth, but basic tips which will be useful to beginners, and which we may often overlook. As I do Java in my daily work, so you'll probably see many "Java Best Practices". The main motivation of sharing is not to "showoff" (anyway, there's nothing to), but hopefully to help java programming beginners code better, and remind myself what I learnt. It is not meant to replace any books but to create more fun for the readers and myself. Tech books are still one of the best resource to learn. And if you are reading this blog, and thinking "hey this is chicken feet lah, you don't know all these meh?", I guess the content is too low-level for you. But if you think it helps you, or you find any mistakes in the posts, please feel free to comment! Hope to update the site design, and post the first very soon (once I get a bit of time off work)! Meantime, you can visit my regular blog. =) Oh yah, one more thing, I like the name of this space. Creative and well-thought! Do you think so too? Cheers!

Archives

website statistics links to this post