和风细雨

世上本无难事,心以为难,斯乃真难。苟不存一难之见于心,则运用之术自出。

表格渲染器示例



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

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

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

/**
 * 间隔色表格渲染类
 * 
@author: sitinspring(junglesong@gmail.com)
 * @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; 
       }
 
}
 



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(junglesong@gmail.com)
 * @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; 
       }
 
}
 

posted on 2008-02-22 10:06 和风细雨 阅读(400) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: