• A great enhancement to the CellStyle feature in JIDE Grids

    by  • January 17, 2012 • Java and Swing, JIDE

    A plain JTable is boring. People usually want to add some styles such as different colors, different fonts, nice little decorations to the JTable cells to make the table more interesting. Before using JIDE Grids, the only way to add styles is to use customized TableCellRenderers. For each unique style, create a TableCellRenderer and associate it with the table cells either by either mapping it to a data type or override JTable’s getCellRenderer  method. For a table with a lot of unique styles, you will end up with a lot of customized cell renderers which is clearly not fun at all.

    The CellStyle feature in JIDE Grids provides a hook into the cell renderer creation process. You use CellStyle to pass in some predefined styles, such as font, font style, background, foreground, border, alignments. If you are using a CellStyleTable or one of its subclasses, those styles will be applied to the renderer component before it is used to paint the cell. The DefaultTableCellRenderer is a JLabel. As long as you are okay to use JLabel to renderer the cell, the cell styles feature would work so that you don’t have to introduce customized cell renderers.

    There are cases that JLabel can’t handle the painting of the cells. For example, a cell that renders a five-star rating. Clearly JLabel can’t handle it. And it sounds like for sure that you will have to create a customized renderer based on a five-star rating component. But we have another way to do it. Always keep in mind, JTable uses the renderer component to paint the cell. If we can find a way to use our own code to do the painting, we may not need a new cell renderer after all. That’s why we decide to bring the CellStyle feature to the next level by introducing CellPainter.

    CellPainter is a simple interface like this.

    public interface CellPainter {
        void paint(Graphics g, Component component, int row, int column, Rectangle cellRect, Object value);
    }

    We introduced two types of CellPainters to the CellStyleTable. One is the OverlayCellPainter, the other is the UnderlayCellPainter. We paint the cell in three steps. First, the lower layer, which is painted before the cell content, paints using the underlay CellPainter specified in the CellStyle. The second step is the to paint the cell content using the renderer component, as the table does right now. The last step is to paint using the overlay CellPainter after the cell content is painted. By leveraging this three-step painting, we make a lot of features possible.

    The demo below shows both cell painters. The underlay painter is used for the “Last” column. It paints a gradient bar based on the value. The length of the bar matches the value. The color of bar is also changed based on the range of the value. You can paint the five-star rating using this painter too.

    The overlay painter can be used to paint something that is over the cell value, i.e., to indicate the value is abnormal, invalid, or should be emphasized etc. You can see the column “Volume” having such an overlay painter where we randomly paint an icon for some cells as an overlay. In the JIDE Common Layer, we have Overlay component which can place an overlay component over another component. It is perfect for JTextField, JComboBox etc. small component to show a validation error. However it is so useful in the case of JTable cells. The CellStyle overlay painter feature is here to fill in the gap. You can now easily display validation errors using this new feature.

    One Response to A great enhancement to the CellStyle feature in JIDE Grids

    1. Pingback: Java desktop links of the week, January 24 | Jonathan Giles