The Java Developers Almanac 1.4


Order this book from Amazon.

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

e928. Creating a Custom Cell Renderer in a JTable Component

A table cell renderer needs to implement a single method, TableCellRenderer.getTableCellRendererComponent() that returns a component. For performance reasons, the renderer should not create a new component each time getTableCellRendererComponent() is called. Rather, it should return the same component (or one from a set) every time. Typically, the renderer can either hold onto a component instance and return that component or it can be a subclass of a component and return itself.

The job of getTableCellRendererComponent() is to configure the component based on the coordinates and value in the cell. The table then uses the configured component and paints it on the screen. After painting it, the table no longer needs the component.

    JTable table = new JTable();
    // Add data...
    
    // Install the custom renderer on the first visible column
    int vColIndex = 0;
    TableColumn col = table.getColumnModel().getColumn(vColIndex);
    col.setCellRenderer(new MyTableCellRenderer());
    
    // This renderer extends a component. It is used each time a
    // cell must be displayed.
    public class MyTableCellRenderer extends JLabel implements TableCellRenderer {
        // This method is called each time a cell in a column
        // using this renderer needs to be rendered.
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
            // 'value' is value contained in the cell located at
            // (rowIndex, vColIndex)
    
            if (isSelected) {
                // cell (and perhaps other cells) are selected
            }
    
            if (hasFocus) {
                // this cell is the anchor and the table has the focus
            }
    
            // Configure the component with the specified value
            setText(value.toString());
    
            // Set tool tip if desired
            setToolTipText((String)value);
    
            // Since the renderer is a component, return itself
            return this;
        }
    
        // The following methods override the defaults for performance reasons
        public void validate() {}
        public void revalidate() {}
        protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {}
        public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}
    }

 Related Examples
e926. Getting and Setting a Cell Value in a JTable Component
e927. Using Built-In Cell Renderers and Editors 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.