The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.table  [62 examples] > Cells  [5 examples]

e927. Using Built-In Cell Renderers and Editors in a JTable Component

There are built-in cell renderers and editors available for seven types: Boolean, Date, Double, Float, Icon, Number, and Object. By default every column uses the Object cell renderer and editor, regardless of the column data type. There are two typical ways to force a column to use a particular cell renderer and editor.

The first way is to have the table model's getColumnClass() method return the Class object for the column data type. Typically, this method is overridden to return the Class object of the first cell in that column. The Class object is then used to fetch the appropriate cell renderer and editor.

The second way is to find the TableColumn associated with the column data in the table model and explicitly set a cell renderer and editor. This method must be used if two columns have different cell renderers yet their data type is the same.

    DefaultTableModel model = new DefaultTableModel() {
        // This method returns the Class object of the first
        // cell in specified column in the table model.
        // Unless this method is overridden, all values are
        // assumed to be the type Object.
        public Class getColumnClass(int columnIndex) {
            Object o = getValueAt(0, columnIndex);
            if (o == null) {
                return Object.class;
            } else {
                return o.getClass();
            }
        }
    };
    JTable table = new JTable(model);
    
    // Add a column for each of the available renderers
    model.addColumn("Boolean", new Object[]{Boolean.TRUE});
    model.addColumn("Date", new Object[]{new Date()});
    model.addColumn("Double", new Object[]{new Double(Math.PI)});
    model.addColumn("Float", new Object[]{new Float(1.2)});
    model.addColumn("Icon", new Object[]{new ImageIcon("icon.gif")});
    model.addColumn("Number", new Object[]{new Integer(1)});
    model.addColumn("Object", new Object[]{"object"});
    
    // Explicitly set the Boolean cell renderer and editor on the
    // TableColumn showing the first column in the table model
    int mColIndex = 0;
    findTableColumn(table, mColIndex).setCellRenderer(table.getDefaultRenderer(Boolean.class));
    findTableColumn(table, mColIndex).setCellEditor(table.getDefaultEditor(Boolean.class));
    
    // Returns the TableColumn associated with the specified column
    // index in the model
    public TableColumn findTableColumn(JTable table, int columnModelIndex) {
        Enumeration enum = table.getColumnModel().getColumns();
        for (; enum.hasMoreElements(); ) {
            TableColumn col = (TableColumn)enum.nextElement();
            if (col.getModelIndex() == columnModelIndex) {
                return col;
            }
        }
        return null;
    }

 Related Examples
e926. Getting and Setting a Cell Value in a JTable Component
e928. Creating a Custom Cell Renderer in a JTable Component
e929. Creating a Class-Based Custom Cell Renderer in a JTable Component
e930. Copying Cell Values to the Clipboard from a JTable Component

See also: Column Heads    Columns    Editing    Events    Layout    Rows    Scrolling    Selection    Sorting    Table Model    Tool Tips   


© 2002 Addison-Wesley.