Category Archives: Design

Stop hardcoding column numbers in your TableModel

If you always been using column numbers when implementing your TableModel, you’ll know that it a burden when you have to change the order of the columns.

If you normally do the following in your TableModel implementation:

    @Override public String getColumnName(int column) {
        switch(column) {
            case 0: return "Closing Date";
            case 1: return "Open Price";
            case 2: return "Day High";
            case 3: return "Day Low";
            case 4: return "Close< Price";
        }
        return null;
    }

You'll have to go through the source code and re-number each column whenever you need to change the order of the columns. There will be multiple places to update, such as the getColumnName and getValueAt methods. If you do complex rendering then you will also need to update the TableCellRenderer implementation as well.

To avoid hardcoding the column numbers, you can use enums. Enums are both more descriptive and have ordinal values.

READ MORE »

Improving the Swing GUI with seamless textured background images

With the simple use of seamless textured background images, you can make a dramatic improvement in the appearance of your Swing GUI. Seamless textures are great because you can tile them horizontally and vertically to fit any component size.

Here are some more examples with different textures:

READ MORE »

Command Chaining: From JQuery to Swing

JQuery is my favorite Javascript library for developing web-based applications. One of the things I really like is the ability chain commands together to reduce the code size. Here is an example from JQuery to hide and add an event handler to an HTML element.

$(document).ready(function() {
   $('#faq').find('dd').hide().end().find('dt').click(function() {
     $(this).next().slideToggle();
   });
 });

Inspired by the JQuery, I started to use the same command chaining technique in my Swing programs and libraries. I been spending some time developing an animation library designed with command chaining at the core.
READ MORE »