How to change header background color of a table in Java


To change header background color, at first get the header background −

JTableHeader tableHeader = table.getTableHeader();

Now, set the background color using set Background() −

tableHeader.setBackground(Color.black);

Above, we have used the Color class to set the color.

The following is an example to change the header background color of a JTable −

Example

package my;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
public class SwingDemo {
   public static void main(String[] argv) throws Exception {
      Integer[][] marks = {
         { 70, 66, 76, 89, 67, 98 },
         { 67, 89, 64, 78, 59, 78 },
         { 68, 87, 71, 65, 87, 86 },
         { 80, 56, 89, 98, 59, 56 },
         { 75, 95, 90, 73, 57, 79 },
         { 69, 49, 56, 78, 76, 77 }
      };
      String col[] = { "S1", "S2", "S3", "S4", "S5", "S6"};
      JTable table = new JTable(marks, col);
      Font font = new Font("Verdana", Font.PLAIN, 12);
      table.setFont(font);
      table.setRowHeight(30);
      table.setBackground(Color.blue);
      table.setForeground(Color.white);
      JTableHeader tableHeader = table.getTableHeader();
      tableHeader.setBackground(Color.black);
      tableHeader.setForeground(Color.white);
      JFrame frame = new JFrame();
      frame.setSize(600, 400);
      frame.add(new JScrollPane(table));
      frame.setVisible(true);
   }
}

The output is as follows. Here, we have changed the header background color to BLACK −

Output

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements