View RSS Feed

Java Swing

Using Custom Renderers

Rate this Entry
by , 04-26-2012 at 08:45 PM (528 Views)
This section shows how cell renderer could be specified or created. “Type specific” cell renderer could be set by the help of JTable setDefaultRenderer method. Cells could be specified in a specific column by using a renderer. However, TableColumn method setCellRenderer is used usually by people. A “Cell specific” renderer could be specified by creation of subclass JTable.
Image or text could easily be customized that are rendered by DefaultTableCellRenderer. Just one subclass is created and setValue method is being implemented so setIcon or setText is invoked along with appropriate image or string. Default date renderer gets implemented as following:
Java Code:
static class DateRenderer extends DefaultTableCellRenderer {
    DateFormat formatter;
    public DateRenderer() { super(); }

    public void setValue(Object value) {
        if (formatter==null) {
            formatter = DateFormat.getDateInstance();
        setText((value == null) ? "" : formatter.format(value));

Submit "Using Custom Renderers" to Facebook Submit "Using Custom Renderers" to Digg Submit "Using Custom Renderers" to Submit "Using Custom Renderers" to StumbleUpon Submit "Using Custom Renderers" to Google