How can I change JTable's header background color?
Asked Answered
M

7

11

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.

Marrow answered 15/10, 2011 at 16:7 Comment(3)
See also How can I put a control in the JTableHeader of a JTable?.Ancestry
This has nothing to do with NetBeans keeping some default color and all to do with a bug in your code, but with just the statement "this code doesn't work in my project only", and without any code, preferably an sscce, I doubt that any of us can give you any more help other than to suggest you try to debug a little deeper. Perhaps the table you're trying to change the header color on isn't the same table object that is displayed, but again without understandable and preferably compilable code, this is just a SWAG on my part?Leaky
To change the default color: UIManager.put("TableHeader.background", Color.ORANGE);.Howarth
M
1

Solved it. In NetBeans:

  • Right click on project's name
  • Properties
  • Application - Desktop App
  • Look and Feel: choose 'Java Default' (didn't work with System Default)
  • Remember to Clean And Rebuild before running project

Also the graphics of the whole project changed appearance.

Marrow answered 15/10, 2011 at 17:52 Comment(4)
It looks like this option is only available for NetBeans projects of type Java Desktop Application. Consider updating the question to include the tag jsr296.Ancestry
I still think that it can be solved in code, that you may not be setting the property for the correct reference.Leaky
I suggest that you jar your project with source code and upload it somewhere, then let us look at it.Leaky
Thank you. I hate when netbeans settings overwrite my explicit changes like this. PS, I get the same behavior as OP and I don't think it's his code. It's that netbeans GUI builder sets UI properties in multiple places in your codebase. If you use the GUI builder, these will be fairly fragmented, so it's hard to copy and paste it into something tractagble in SOBondholder
C
15

Try this:

table.getTableHeader().setOpaque(false);

then set the background of jtable header

table.getTableHeader().setBackground(Color.BLACK);
Crannog answered 27/11, 2014 at 9:2 Comment(0)
L
12

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.

Leaky answered 15/10, 2011 at 16:15 Comment(1)
it works indeed. For my table though it doesn't work. See EDIT.Marrow
C
10

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);
}
Chloride answered 7/3, 2013 at 19:59 Comment(0)
S
4

The table header also uses a renderer component, like table cells.

Look at this:

table.getTableHeader().setDefaultRenderer(new DefaultTableRenderer(){
  {
    // you need to set it to opaque
    setOpaque(true);
  }

@Override
public Component getTableCellRendererComponent(final JTable table,
  final Object value, final boolean isSelected, final boolean hasFocus,
  final int row, final int column) {
    // set the background
    setBackground(yourDesiredColor);
  }
});

If you do not need a dynamic color, you can also set the color in the constructor of the renderer.

Schleicher answered 16/10, 2011 at 7:44 Comment(0)
M
1

Solved it. In NetBeans:

  • Right click on project's name
  • Properties
  • Application - Desktop App
  • Look and Feel: choose 'Java Default' (didn't work with System Default)
  • Remember to Clean And Rebuild before running project

Also the graphics of the whole project changed appearance.

Marrow answered 15/10, 2011 at 17:52 Comment(4)
It looks like this option is only available for NetBeans projects of type Java Desktop Application. Consider updating the question to include the tag jsr296.Ancestry
I still think that it can be solved in code, that you may not be setting the property for the correct reference.Leaky
I suggest that you jar your project with source code and upload it somewhere, then let us look at it.Leaky
Thank you. I hate when netbeans settings overwrite my explicit changes like this. PS, I get the same behavior as OP and I don't think it's his code. It's that netbeans GUI builder sets UI properties in multiple places in your codebase. If you use the GUI builder, these will be fairly fragmented, so it's hard to copy and paste it into something tractagble in SOBondholder
F
0
private void table_head_color(JTable table_name){
    DefaultTableCellRenderer head_render = new DefaultTableCellRenderer(); 
        
    head_render.setBackground(new Color(204,153,255));
    table_name.getTableHeader().setDefaultRenderer(head_render);

    //to call above method
    //table_head_color("write table name");
}
Frozen answered 5/1, 2022 at 19:21 Comment(0)
B
-2

This question is old but I have the right answer, I guess...

All that you have to do is set opaque to true.

e.g.

yourComponent.setOpaque(true);

I hope this can help someone in the future.

Bivens answered 22/7, 2020 at 5:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.