How can I change JTable's header background color?

こ雲淡風輕ζ 提交于 2019-12-06 21:29:57

问题


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.


回答1:


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.




回答2:


Try this... table.getTableHeader().setOpaque(false);

then set the background of jtable header

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




回答3:


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);
}



回答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.




回答5:


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.



来源:https://stackoverflow.com/questions/7778958/how-can-i-change-jtables-header-background-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!