001         package com.croftsoft.core.gui.table;
002    
003         import java.awt.*;
004         import javax.swing.*;
005         import javax.swing.table.*;
006    
007         /*********************************************************************
008         * Displays every other JTable row in a different color for contrast.
009         *
010         * <p>
011         * Example:
012         * <pre>
013         * jTable.setDefaultRenderer (
014         *   Object.class,
015         *   new AlternatingRenderer (
016         *     Color.black,
017         *     new Color ( 204, 255, 204 ),
018         *     Color.black,
019         *     new Color ( 230, 230, 190 ),
020         *     Color.black,
021         *     Color.white ) );
022         * </pre>
023         * </p>
024         *
025         * <p>
026         * <b>Reference</b><br />
027         * John Zukowski,
028         * <a target="_blank" href=
029         *   "http://www.amazon.com/exec/obidos/ASIN/189311578X/croftsoft-20">
030         * Definitive Guide to Swing for Java 2, Second Edition</a>,
031         * 2000, Chapter 17, p715.
032         * </p>
033         *
034         * @version
035         *   2001-08-16
036         * @since
037         *   2001-08-16
038         * @author
039         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
040         *********************************************************************/
041    
042         public final class  AlternatingRenderer
043           implements TableCellRenderer
044         //////////////////////////////////////////////////////////////////////
045         //////////////////////////////////////////////////////////////////////
046         {
047    
048         private final Color  selectedForegroundColor;
049    
050         private final Color  selectedBackgroundColor;
051    
052         private final Color  oddForegroundColor;
053    
054         private final Color  oddBackgroundColor;
055    
056         private final Color  evenForegroundColor;
057    
058         private final Color  evenBackgroundColor;
059    
060         //      
061    
062         private final DefaultTableCellRenderer  defaultTableCellRenderer;
063    
064         //////////////////////////////////////////////////////////////////////
065         //////////////////////////////////////////////////////////////////////
066    
067         /*********************************************************************
068         * If a Color argument is null, the default Color will be used.
069         *********************************************************************/
070         public  AlternatingRenderer (
071           Color    selectedForegroundColor,
072           Color    selectedBackgroundColor,
073           Color    oddForegroundColor,
074           Color    oddBackgroundColor,
075           Color    evenForegroundColor,
076           Color    evenBackgroundColor )
077         //////////////////////////////////////////////////////////////////////
078         {
079           this.selectedForegroundColor = selectedForegroundColor;
080    
081           this.selectedBackgroundColor = selectedBackgroundColor;
082    
083           this.oddForegroundColor      = oddForegroundColor;
084    
085           this.oddBackgroundColor      = oddBackgroundColor;
086    
087           this.evenForegroundColor     = evenForegroundColor;
088    
089           this.evenBackgroundColor     = evenBackgroundColor;
090    
091           //
092    
093           defaultTableCellRenderer = new DefaultTableCellRenderer ( );
094         }
095    
096         //////////////////////////////////////////////////////////////////////
097         //////////////////////////////////////////////////////////////////////
098    
099         public Component  getTableCellRendererComponent (
100           JTable   jTable,
101           Object   value,
102           boolean  isSelected,
103           boolean  hasFocus,
104           int      row,
105           int      column )
106         //////////////////////////////////////////////////////////////////////
107         {
108           Component  component
109             = defaultTableCellRenderer.getTableCellRendererComponent (
110             jTable,
111             value,
112             isSelected,
113             hasFocus,
114             row,
115             column );
116    
117           ( ( JLabel ) component ).setOpaque ( true );
118    
119           Color  foregroundColor;
120    
121           Color  backgroundColor;
122    
123           if ( isSelected )
124           {
125             foregroundColor = selectedForegroundColor;
126    
127             backgroundColor = selectedBackgroundColor;
128           }
129           else
130           {
131             if ( row % 2 == 0 )
132             {
133               foregroundColor = evenForegroundColor;
134    
135               backgroundColor = evenBackgroundColor;
136             }
137             else
138             {
139               foregroundColor = oddForegroundColor;
140    
141               backgroundColor = oddBackgroundColor;
142             }
143           }
144    
145           if ( foregroundColor != null )
146           {
147             component.setForeground ( foregroundColor );
148           }
149    
150           if ( backgroundColor != null )
151           {
152             component.setBackground ( backgroundColor );
153           }
154    
155           return component;
156         }
157    
158         //////////////////////////////////////////////////////////////////////
159         //////////////////////////////////////////////////////////////////////
160         }