I have a JTable
in my program and I wanted to change the color of the JTableHeader
. I did this using the following code
JTableHeader header = table.getTableHeader();
header.setBackground(Color.WHITE);
However, when I dragged the header, I noticed that there was a grey area behind the header as shown in the photo below.
How can I set this to white so that it fits in with my JTableHeader
?
MCVE
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class JTableTest extends JFrame {
private JTableTest() {
super("JTable Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1, 1));
createPanel();
pack();
setVisible(true);
}
JPanel panel = new JPanel(new GridLayout(1, 1));
JScrollPane scroll = new JScrollPane();
private void createPanel() {
Object[] headers = {"Select", "Title", "Artist", "Length"};
Object[][] sampleData = {{true, "Bat Outta Hell", "Meat Loaf", "673"},
{false, "Spanish Train", "Chris De Burgh", "358"}};
JTable table = new JTable(sampleData, headers);
///
JTableHeader header = table.getTableHeader();
header.setBackground(Color.WHITE); //Sets header white
///
scroll.getViewport().add(table);
scroll.getViewport().setBackground(Color.WHITE); //Sets table container white
panel.add(scroll);
panel.setBackground(Color.WHITE); //Sets scroll pane container white
getContentPane().add(panel);
getContentPane().setBackground(Color.WHITE); //Sets panel container white
//What should be set to white to make header container white
}
public static void main(String[] args) {
SwingUtilities.invokeLater (
new Runnable() {
@Override
public void run() {
new JTableTest();
}
}
);
}
}