[原创]对表格行颜色进行渲染(代码示范)
/**
* @(#) EditorRenderer.java
*
* Copyright 2004 Opensource Develop Team. All rights reserved.
*/
// package
package com.opensource.table;
// import classes
import javax.swing.table.TableCellRenderer;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.*;
import java.awt.*;
/**
* 一个表格渲染器。作用是使得选中的行的颜色和别的行的颜色不同。
*
* @author: ODT
* @see: DefaultCellRenderer
* @version: 1.0 22/04/2004
* @since: 1.3
*/
public class EditorRenderer extends DefaultTableCellRenderer
{
// 表格内部有JCheckBox组件
JCheckBox checkBox = new JCheckBox();
JTable table;
public EditorRenderer(JTable table)
{
this.table = table;
}
public Component getTableCellRendererComponent
(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column)
{
if (row == table.getSelectedRow())
{
checkBox.setBackground(new Color(187, 137, 233));
}
else
{
checkBox.setBackground(new Color(244, 244, 128));
}
checkBox.setSelected(((Boolean)value).booleanValue());
checkBox.setHorizontalAlignment(JLabel.CENTER);
return checkBox;
}
}
本文地址:http://com.8s8s.com/it/it15833.htm