//下面是我写的复选框render,你可以直接用 package com.sbt.common.sbttable;import java.awt.Color; import java.awt.Component;import javax.swing.JCheckBox; import javax.swing.JTable; import javax.swing.UIManager; import javax.swing.border.Border; import javax.swing.table.DefaultTableCellRenderer;/** * @author yhli * Created on 2005-12-12 */ public class JCheckBoxTableCellRender extends DefaultTableCellRenderer { private JCheckBox c;
// implements javax.swing.table.TableCellRenderer /** * * Returns the default table cell renderer. * * @param table the <code>JTable</code> * @param value the value to assign to the cell at * <code>[row, column]</code> * @param isSelected true if cell is selected * @param hasFocus true if cell has focus * @param row the row of the cell to render * @param column the column of the cell to render * @return the default table cell renderer */ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (c == null) c = new JCheckBox(); if (isSelected) { c.setForeground(table.getSelectionForeground()); c.setBackground(table.getSelectionBackground()); } else { c.setForeground(table.getForeground()); c.setBackground(table.getBackground()); } c.setFont(table.getFont()); if (hasFocus) { Border border = null; if (isSelected) { border = UIManager.getBorder("Table.focusSelectedCellHighlightBorder"); } if (border == null) { border = UIManager.getBorder("Table.focusCellHighlightBorder"); } c.setBorder(border); if (!isSelected && table.isCellEditable(row, column)) { Color col; col = UIManager.getColor("Table.focusCellForeground"); if (col != null) { c.setForeground(col); } col = UIManager.getColor("Table.focusCellBackground"); if (col != null) { c.setBackground(col); } } } else { c.setBorder(noFocusBorder); } String value_ = value == null ? "" : value.toString(); boolean isSelected_ = new Boolean(value_).booleanValue(); c.setSelected(isSelected_);
package com.sbt.common.sbttable;import java.awt.Color;
import java.awt.Component;import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableCellRenderer;/**
* @author yhli
* Created on 2005-12-12
*/
public class JCheckBoxTableCellRender extends DefaultTableCellRenderer
{ private JCheckBox c;
// implements javax.swing.table.TableCellRenderer
/**
*
* Returns the default table cell renderer.
*
* @param table the <code>JTable</code>
* @param value the value to assign to the cell at
* <code>[row, column]</code>
* @param isSelected true if cell is selected
* @param hasFocus true if cell has focus
* @param row the row of the cell to render
* @param column the column of the cell to render
* @return the default table cell renderer
*/
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column)
{
if (c == null)
c = new JCheckBox(); if (isSelected)
{
c.setForeground(table.getSelectionForeground());
c.setBackground(table.getSelectionBackground());
}
else
{
c.setForeground(table.getForeground());
c.setBackground(table.getBackground());
} c.setFont(table.getFont()); if (hasFocus)
{
Border border = null;
if (isSelected)
{
border = UIManager.getBorder("Table.focusSelectedCellHighlightBorder");
}
if (border == null)
{
border = UIManager.getBorder("Table.focusCellHighlightBorder");
}
c.setBorder(border); if (!isSelected && table.isCellEditable(row, column))
{
Color col;
col = UIManager.getColor("Table.focusCellForeground");
if (col != null)
{
c.setForeground(col);
}
col = UIManager.getColor("Table.focusCellBackground");
if (col != null)
{
c.setBackground(col);
}
}
}
else
{
c.setBorder(noFocusBorder);
}
String value_ = value == null ? "" : value.toString();
boolean isSelected_ = new Boolean(value_).booleanValue();
c.setSelected(isSelected_);
// //设置行的颜色---yhli@2005-12-16
// if (row % 2 == 0)
// c.setBackground(Color.white); //设置奇数行底色
// else if (row % 2 == 1)
// c.setBackground(new Color(206, 231, 255)); //设置偶数行底色
// //设置被选中的颜色为红色---yhli@2005-12-16
// if (isSelected_)
// {
// c.setForeground(table.getSelectionForeground());
// c.setBackground(Color.RED);
// }
// else
// {
// c.setForeground(table.getForeground());
// c.setBackground(table.getBackground());
// } return c;
}}
JCheckBox selBox = new JCheckBox();
JTableColumn selBoxColumn = getTableMng().getJTableColumn(SELECTION);
JTableCellEditor editor = new JTableCellEditor(selBox);
selBoxColumn.setCellEditor(editor);
//设置"选择生成采购单列"单元格构造
JCheckBoxTableCellRender render = new JCheckBoxTableCellRender();
selBoxColumn.getTableColumn().setCellRenderer(render);