2 Replies Latest reply: May 23, 2012 11:04 AM by 939186 RSS

    Problem making JTableHeader transparent


      I have a problem with JTables, in particular the JTableHeader. I can't seem to make it transparent. I have a JTable placed on top of a panel with a gradient color, and I want the table to be transparent so the gradient will be visible through the table's content.

      I have used setOpaque(false) on both the JTable and its header, and I have made a transparent TableCellRenderer which I use for both the table's cells and its header. Although it works for the table's content area, the header still gets painted as a solid rectangle. I have even tried overriding the look and feel for the JTable and JTableHeader, but nothing I do seems to have any effect.

      When googling the problem i find a lot of information about making JTable transparent, but I can't seem to find any relevant information about the table header in particular.

      Heres a SSCCE to better illustrate what I've tried so far (no changes are made to the look and feel in this example):

      import java.awt.Color;
      import java.awt.Component;
      import java.awt.GradientPaint;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.JTable;
      import javax.swing.SwingConstants;
      import javax.swing.border.EmptyBorder;
      import javax.swing.table.DefaultTableColumnModel;
      import javax.swing.table.DefaultTableModel;
      import javax.swing.table.JTableHeader;
      import javax.swing.table.TableCellRenderer;
      import javax.swing.table.TableColumn;
      public class Main {
          private static TransparentRenderer transparentCellRenderer = new TransparentRenderer();
          public static void main(String[] args) {
              JFrame frame = new JFrame();
              frame.setSize(800, 600);
              //Creating the content pane with a gradient painted background
              JPanel gradientBackgroundPanel = new JPanel() {
                  public void paintComponent(Graphics g) {
                      Graphics2D g2d = (Graphics2D) g;
                      g2d.setPaint(new GradientPaint(0f, 0f, Color.green, 0f, getHeight(), Color.blue));
                      g2d.fillRect(0, 0, getWidth(), getHeight());
              //Initializing the table
              JTable table = createTable();
              //Adding the table to a transparent scroll pane
              JScrollPane scrollPane = new JScrollPane(table);
              scrollPane.setBorder(new EmptyBorder(10, 10, 10, 10));
              //Adding the scroll pane to the content pane and setting the frame visible
          private static JTable createTable() {
              //Creating the table column model with a single column using a transparent renderer
              DefaultTableColumnModel columnModel = new DefaultTableColumnModel();
              TableColumn column = new TableColumn();
              //Creating the table header with a transparent renderer
              JTableHeader tableHeader = new JTableHeader(columnModel);
              //Creating a table model
              DefaultTableModel tableModel = createTableModel();
              //Creating the JTable
              JTable table = new JTable(tableModel);
              //Making both the table and the table header transparent
              return table;
          protected static DefaultTableModel createTableModel() {
              DefaultTableModel tableModel = new DefaultTableModel(5, 1);
              tableModel.setValueAt("Entry 1", 0, 0);
              tableModel.setValueAt("Entry 2", 1, 0);
              tableModel.setValueAt("Entry 3", 2, 0);
              tableModel.setValueAt("Entry 4", 3, 0);
              tableModel.setValueAt("Entry 5", 4, 0);
              return tableModel;
          private static class TransparentRenderer extends JLabel implements TableCellRenderer {
              public TransparentRenderer() {
              public Component getTableCellRendererComponent(JTable table, Object value,
                      boolean isSelected, boolean hasFocus, int row, int column) {
                  return this;
      Any help would be very much appreciated.