表格渲染器示例

表格渲染器示例

表格渲染器示例_第1张图片

实现类:
package  com.sitinspring.view.panel;

import  java.awt.Color;
import  java.awt.Component;

import  javax.swing.JTable;
import  javax.swing.table.DefaultTableCellRenderer;

/** */ /**
 * 间隔色表格渲染类
 * 
@author: sitinspring([email protected])
 * @date: 2008-1-10
 
*/

public   class  ColorTableCellRenderer  extends  DefaultTableCellRenderer 
    
private static final long serialVersionUID = -3378036327580475639L;

    
public Component getTableCellRendererComponent( 
               JTable table, 
               Object value, 
               
boolean isSelected, 
               
boolean hasFocus, 
               
int row, 
               
int column) 
        
               
// 得到渲染的单元格
               Component cell = 
                       
super.getTableCellRendererComponent( 
                               table, 
                               value, 
                               isSelected, 
                               hasFocus, 
                               row, 
                               column); 

               
// 进行渲染
               if (hasFocus) 
                   
// 如果获得焦点则设置背景色为红色
                   cell.setBackground(Color.red); 
                   
//cell.setForeground(Color.black); 
               }
 else 
                   
if ((row % 2== 0
                       
// 偶数行设置为白色
                       cell.setBackground(Color.white); 
                   }
 else 
                       
// 奇数行设置为蓝色
                       cell.setBackground(Color.cyan); 
                   }
 
               }
 
               
               
return cell; 
       }
 
}
 


表格渲染器示例_第2张图片
package  com.sitinspring.view.panel;

import  java.awt.Color;
import  java.awt.Component;
import  java.util.regex.Pattern;

import  javax.swing.JTable;
import  javax.swing.table.DefaultTableCellRenderer;

/** */ /**
 * 大於37顯示紅色的表單元格
 * 
@author: sitinspring([email protected])
 * @date: 2008-1-10
 
*/

public   class  AgeTableCellRenderer  extends  DefaultTableCellRenderer 
    
private static final long serialVersionUID = -334535475639L;

    
public Component getTableCellRendererComponent( 
               JTable table, 
               Object value, 
               
boolean isSelected, 
               
boolean hasFocus, 
               
int row, 
               
int column) 
        
               
// 得到单元格
               Component cell = 
                       
super.getTableCellRendererComponent( 
                               table, 
                               value, 
                               isSelected, 
                               hasFocus, 
                               row, 
                               column); 
               
               
// 先把所有单元格设置为白色
               cell.setBackground(Color.white); 
               
               
// 进行渲染
               if (table.getColumnName(column).equals("年龄") ) // 如果列名等于“年龄”
                   
// 取得单元格的文字
                   String strValue=(String)value;
                   
                   
if(Pattern.matches("\\d+", strValue)){
                       
if(Integer.parseInt(strValue)>37){
                           
// 如果是数字且值大于37,将单元格背景设置为红色
                           cell.setBackground(Color.red); 
                       }
                       
                   }

               }
 
               
               
return cell; 
       }
 
}
 

你可能感兴趣的:(表格渲染器示例)