How can I change JTable's header background color?

It works for me. Here's my SSCCE:

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableHeaderBackground {
   public static void main(String[] args) {
      Integer[][] data = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
      String[] cols = {"A", "B", "C"};

      JTable table = new JTable(data, cols);

      JTableHeader header = table.getTableHeader();
      header.setBackground(Color.black);
      header.setForeground(Color.yellow);

      JOptionPane.showMessageDialog(null, new JScrollPane(table));
   }
}

If this doesn't help you, then I suggest that you create and post your own SSCCE so that we can see what's wrong.


Try this:

table.getTableHeader().setOpaque(false);

then set the background of jtable header

table.getTableHeader().setBackground(Color.BLACK);

I recommend you to do this:

DefaultTableCellRenderer headerRenderer = new DefaultTableCellRenderer();
headerRenderer.setBackground(new Color(239, 198, 46));

for (int i = 0; i < myJTable.getModel().getColumnCount(); i++) {
        myJTable.getColumnModel().getColumn(i).setHeaderRenderer(headerRenderer);
}