Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click NOW to join Planet IT!
Click NOW to join Planet IT!
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


The table created using the enhanced renderers is shown in Figure 12.7.


Figure 12.7  Enhanced renderers in the JTable.

Listing 12.7 Enhanced Boolean and Color Class TableCellRenderers

/**
 * The SelectedCheckBoxRenderer class is almost the same as the
 * default check box renderer in the JTable class. However, it
 * paints the cell in the normal table selected colors when it is
 * selected. This avoids the white areas in the table for Boolean
 * columns. Also, if all columns in the table are Boolean, you
 * wouldn’t be able to see the selected row using the default renderer!
 *
 * @author Mike Foley
 **/
class SelectedCheckBoxRenderer extends JCheckBox
    implements TableCellRenderer {

    private Border noFocusBorder;
    private Color unselectedForeground;
    private Color unselectedBackground;
    /**
     * SelectedCheckBoxRenderer, default constructor.
     *
     * We need to be opaque so our background color is painted.
     * Create our border. Keep a reference around so it can be
     * reset when we are not selected.
     * Center the check box to match the position of the Boolean editor.
     **/
    public SelectedCheckBoxRenderer() {
        super();
setOpaque( true );

        noFocusBorder = new EmptyBorder( 1, 2, 1, 2 );
        setBorder( noFocusBorder );

        setHorizontalAlignment( JLabel.CENTER );
    }

    /**
     * Set the foreground color. Remember the color, so
     * we can reset it when we are not selected.
     *
     * @param c The new foreground color.
     **/
    public void setForeground(Color c) {
        super.setForeground(c);
        unselectedForeground = c;
    }

    /**
     * Set the background color. Remember the color, so
     * we can reset it when we are not selected.
     *
     * @param c The new background color.
     **/
    public void setBackground(Color c) {
        super.setBackground(c);
        unselectedBackground = c;
    }

    /**
     * Clear the foreground and background colors after
     * updating the UI. This will cause the colors to be
     * read from the table property portion of the UI.
     **/
    public void updateUI() {
        super.updateUI();
        setForeground( null );
        setBackground( null );
    }

    /**
     * getTableCellRendererComponent, from TableCellRenderer
     *
     * Configure the check box for the given state.
     *
     **/
    public Component getTableCellRendererComponent( JTable table,
                         Object value, boolean isSelected,
 boolean hasFocus, int row, int column ) {

        //
        // Check or uncheck the JCheckBox.
        //
        setSelected((value != null && ((Boolean)value).booleanValue()));

        //
        // If we are selected, paint in the table’s selection colors.
        //
        if( isSelected ) {
            super.setForeground( table.getSelectionForeground() );
            super.setBackground( table.getSelectionBackground() );
        } else {
            super.setForeground( ( unselectedForeground != null ) ?
                          unselectedForeground : table.getForeground() );
            super.setBackground( ( unselectedBackground != null ) ?
                          unselectedBackground : table.getBackground() );
        } // else

        //
        // If we have the focus, paint in the focus color for the table
        // and set the focus border.
        // If not, set the no focus border.
        //
        if( hasFocus ) {
            setBorder( UIManager.getBorder(
                 “Table.focusCellHighlightBorder” ) );
            if (table.isCellEditable(row, column)) {
                super.setForeground( UIManager.getColor(
                       “Table.focusCellForeground” ) );
                super.setBackground( UIManager.getColor(
                       “Table.focusCellBackground” ) );
            }
        } else {
            setBorder( noFocusBorder );
        }

        return( this );
    }

} // SelectedCheckBoxRenderer

        //
        // Create and register a Color class renderer.
        //
        DefaultTableCellRenderer colorRenderer =
                   new DefaultTableCellRenderer() {
            public void setValue( Object value ) {
                setBackground( ( Color )value );
            }
        };

        colorRenderer.setOpaque( true );
        table.setDefaultRenderer( Color.class, colorRenderer );
        table.setDefaultRenderer( Boolean.class,
                                   new SelectedCheckBoxRenderer() );


NOTE:  
The SelectedCheckBoxRenderer class shown in Listing 12.7 assumes that the value passed to the getTableCellRendererComponent method is a Boolean. This means that if this class is registered as a renderer in a table for a class that is not a Boolean or a descendent of Boolean, the renderer will throw a ClassCastException when used. Similarly, the color renderer assumes that a Color is passed in its setValue method. If the renderer were used to renderer values other than colors, a ClassCastException would be thrown.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.