Prabhu
Prabhu

Reputation: 3538

Class specific renderer component not called

I've a JTable set to display String and Boolean values in the same column. I've the following piece of code to setup renderers for both the object types.

    table.setDefaultRenderer(Boolean.class, new BooleanHandler());
    table.setDefaultRenderer(String.class, new StringHandler());
    table.setDefaultRenderer(
            Object.class,
            new DefaultTableCellRenderer() {
                @Override
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                    System.out.println("Inside overridden function");
                    return super.getTableCellRendererComponent(table, value, isSelected, hasFocus,row, column);
                }
            }
    );

The issue I face is that, the renderer for Object gets called always instead of Boolean or String. I tried removing the renderer for Object, still no luck.

Upvotes: 0

Views: 238

Answers (1)

camickr
camickr

Reputation: 324207

I've a JTable set to display String and Boolean values in the same column

Then you can't just use the normal rendering logic.

Normally the renderer is choosen based on the value returned by the getColumnClass(...) method. However, this is column based, not cell based so you won't know which renderer to return.

Instead you need to override the getCellRenderer(...) and getCellEditor(...) methods to return the renderer/editor based on the data in the cell.

An example of this approach is given below:

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class TablePropertyEditor extends JFrame
{
    public TablePropertyEditor()
    {
        String[] columnNames = {"Type", "Value"};
        Object[][] data =
        {
            {"String", "I'm a string"},
            {"Date", new Date()},
            {"Integer", new Integer(123)},
            {"Double", new Double(123.45)},
            {"Boolean", Boolean.TRUE}
        };

        JTable table = new JTable(data, columnNames)
        {
            private Class editingClass;

            public TableCellRenderer getCellRenderer(int row, int column)
            {
                editingClass = null;
                int modelColumn = convertColumnIndexToModel(column);

                if (modelColumn == 1)
                {
                    Class rowClass = getModel().getValueAt(row, modelColumn).getClass();
                    return getDefaultRenderer( rowClass );
                }
                else
                    return super.getCellRenderer(row, column);
            }

            public TableCellEditor getCellEditor(int row, int column)
            {
                editingClass = null;
                int modelColumn = convertColumnIndexToModel(column);

                if (modelColumn == 1)
                {
                    editingClass = getModel().getValueAt(row, modelColumn).getClass();
                    return getDefaultEditor( editingClass );
                }
                else
                    return super.getCellEditor(row, column);
            }

            //  This method is also invoked by the editor when the value in the editor
            //  component is saved in the TableModel. The class was saved when the
            //  editor was invoked so the proper class can be created.

            public Class getColumnClass(int column)
            {
                return editingClass != null ? editingClass : super.getColumnClass(column);
            }
        };

        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );
    }

    public static void main(String[] args)
    {
        TablePropertyEditor frame = new TablePropertyEditor();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

The above code just uses the default String and Boolean renderers and editors.

The other approach would be to create custom renderers and editors so that each is aware of the two possible data types and returns the appropriate renderer/editor.

Upvotes: 3

Related Questions