Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change JTable's header background color?

I've tried:

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

Doesn't work.

EDIT: This code doesn't work in my project only. Works in other projects. I may have changed a property that stops the color from changing. Or maybe NetBeans has some property which keeps the default colors. I've noticed something else. The color of the header in my project is shining in a different way. In the examples where the color change works, I see different graphics.

EDIT 2: Something else. I noticed that the buttons won't change color either. Must be something generic. Hope this helps. Unfortunately SSCCE won't work in this case, because I can't recreate the problem. I am surely using the right component names.

like image 259
Stefanos Kargas Avatar asked Oct 15 '11 16:10

Stefanos Kargas


People also ask

How to change background color of JTable?

We can change the background and foreground color for each column of a JTable by customizing the DefaultTableCellRenderer class and it has only one method getTableCellRendererComponent() to implement it.

How do I change the background color of a table header?

By default the header of a Tabular report is white. In order to customize the Table header color, you can do it by adding either CSS or Javascript Code. Also, you can use color name or its hex code and can customize the table header color accordingly.

How do I change the background of a JPanel?

We can set a background color to JPanel by using the setBackground() method.

How do you change the background color of a table in Java?

Use setBackground() to specify the background color of an individual TableItem : item2. setBackground(new Color(d,127,178,127));


3 Answers

Try this:

table.getTableHeader().setOpaque(false);

then set the background of jtable header

table.getTableHeader().setBackground(Color.BLACK);
like image 200
Venkatesh Bandarapu Avatar answered Sep 18 '22 20:09

Venkatesh Bandarapu


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.

like image 45
Hovercraft Full Of Eels Avatar answered Sep 19 '22 20:09

Hovercraft Full Of Eels


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);
}
like image 25
Soheil Setayeshi Avatar answered Sep 18 '22 20:09

Soheil Setayeshi