编辑:我删除了这个,因为我认为这不是OP想要的。我在OP的要求下取消删除它。
I have this 8x8 table and and i want to color the grid of the first
and the second cell at the very top of the table with red. My question
is it possible to do this?
当然是。
一种方法是扩展现有的渲染器并覆盖getTableCellRendererComponent方法。
例如:
public class GridlineCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent (
JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column
) {
final Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if ( row == 0 && (column == 0 || column ==1 ) {
cell.setBackground( Color.RED );
}
return cell;
}
}然后,您需要警告您的JTable您希望将此渲染器用于某些类型的数据。
例如,如果要将其用于包含Integer的单元格,则以下内容应该有效:
JTable myJTable = ...
myJTable.setDefaultRenderer(Integer.class, new GridlineCellRenderer() );