Mathias Weyel
Mathias Weyel

Reputation: 819

How to make the header of a JTable transparent

I want to create a JTable that is transparent. The table itself does not impose that much of a problem, I have implemented the TableModel and a TableCellRenderer and set setOpaque(false) on the JTable, the enclosing JScrollPane and the ViewPort of the JScrollPane.

The table now correctly draws headers as it lies in a JScrollPane, so I set my own TableCellRenderer for rendering the header using getTableHeader().setDefaultRenderer(new TaskTableHeaderRenderer());. I want this to be transparent as well, just with text using a JLabel. But try as I might, I can't get it. The Renderer surely provides a transparent component (I even tried to use just a JPanel with setOpaque(false) on it) and I even tried setOpaque(false) on the JTableHeader and setting a transparent background color for all components in question. Nothing seems to help.

The LookAndFeel I am using is the PlasticXPLookAndFeel by JGoodies but I tried Metal and Windows Look & Feel implementations to no avail as well. Furthermore debugging indicates that the PlasticXPLookAndFeel uses the BasicTableHeaderUI without subclassing it.

So how can I achieve transparent table headers?

Upvotes: 2

Views: 3582

Answers (3)

Jim Yeung
Jim Yeung

Reputation: 31

Table.getTableHeader().setOpaque(false);
Table.getTableHeader().setBackground(new Color(0,0,0,0.6f));
Table.getTableHeader().setForeground(Color.white);

Upvotes: 3

javment
javment

Reputation: 368

You can first create an cellRenderer

public class OpaqueHeader extends DefaultTableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable arg0, Object ob,
            boolean arg2, boolean arg3, int arg4, int arg5) {
        JLabel t = new JLabel(ob.toString());
        t.setOpaque(false);
        t.setForeground(Color.black);
        return t;
    }
}

and after in your JTable object

table.getTableHeader().setDefaultRenderer(new OpaqueHeader());

Upvotes: 2

iluxa
iluxa

Reputation: 6969

maybe override paintBackground() and make it not call super.paintBackground()?

Upvotes: -1

Related Questions