JTable table = new JTable(); // Add data... // Install the custom renderer on the first visible column int vColIndex = 0; TableColumn col = table.getColumnModel().getColumn(vColIndex); col.setCellRenderer(new MyTableCellRenderer()); public class MyTableCellRenderer implements TableCellRenderer { // This method is called each time a cell in a column // using this renderer needs to be rendered. public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { // specify the background of cell located at (1, 2) if ((row == 1) && (column == 2)) { setBackground(Color.GRAY); } // 'value' is value contained in the cell located at // (rowIndex, vColIndex) // Configure the component with the specified value setText(value.toString()); // Since the renderer is a component, return itself return this; } }
把你建的 renderer 设置进去,就OK啦。
JTable table = new JTable();
// Add data... // Install the custom renderer on the first visible column
int vColIndex = 0;
TableColumn col = table.getColumnModel().getColumn(vColIndex);
col.setCellRenderer(new MyTableCellRenderer()); public class MyTableCellRenderer implements TableCellRenderer
{
// This method is called each time a cell in a column
// using this renderer needs to be rendered.
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column)
{
// specify the background of cell located at (1, 2)
if ((row == 1) && (column == 2))
{
setBackground(Color.GRAY);
} // 'value' is value contained in the cell located at
// (rowIndex, vColIndex)
// Configure the component with the specified value
setText(value.toString()); // Since the renderer is a component, return itself
return this;
}
}