Creating a JTable with a different background color per column

The trick is to contrive a custom DefaultTableCellRenderer and apply it to individual columns provided by the table’s backing column model. The following snippet demonstrates rendering the first column of the table so it displays with grey background and blue foreground highlighting:

import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame
   public Main() {
      JTable table = new JTable(100, 5);
      TableColumn tm = table.getColumnModel().getColumn(0);
      tm.setCellRenderer(new ColorColumnRenderer(Color.lightGray,;
      getContentPane().add(new JScrollPane(table));
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
   public static void main(String [] args) {
      Main main = new Main();
      main.setSize(400, 400);
* Applied background and foreground color to single column of a JTable
* in order to distinguish it apart from other columns.
class ColorColumnRenderer extends DefaultTableCellRenderer 
   Color bkgndColor, fgndColor;
   public ColorColumnRenderer(Color bkgnd, Color foregnd) {
      bkgndColor = bkgnd;
      fgndColor = foregnd;
   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( bkgndColor );
      cell.setForeground( fgndColor );
      return cell;