Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set table header background

Status
Not open for further replies.

seema165

Programmer
Jul 11, 2005
48
GB
Hi,

I am trying to get my JTable table's header to setOpaque(false) so I can see the background image in the same way I can see it for my table rows and columns.

Can someone please tell me how to do this, if it is even possible to do.

Thanks in advance
:)
 
You might have to subclass the default TableCellRenderer which the JTableHeader uses to render the headers. Have a look at the setDefaultRenderer(TableCellRenderer) method of JTableHeader.

Tim
 
I have a TableCellRenderer class which extends default.
My code is:

public class TennisTableCellRenderer extends DefaultTableCellRenderer {

protected boolean isSelected = false;
protected Color selectionColor;

public TennisTableCellRenderer() {

super();

}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

Component comp = table.getTableHeader().getDefaultRenderer().getTableCellRendererComponent(table,value,isSelected, hasFocus, row, column);

if (comp instanceof JComponent){
((JComponent)comp).setOpaque(false);
((JComponent)comp).setBorder(BorderFactory.createEmptyBorder(2,1,1,1));
//Color oldCol = table.getSelectionBackground();
//selectionColor = new Color(oldCol.getRed(), oldCol.getGreen(), oldCol.getBlue(), 0); //128);
//((JComponent)comp).setBackground(new Color(0,0,0,0));

}
return comp;
}

//public void paintComponent(Graphics g) {
//if (isSelected) {
// g.setColor(selectionColor);
// g.fillRect(0, 0, getWidth(), getHeight());

//}
// super.paintComponent(g);
//}


What I'm trying to do (which I'm not sure if its possible or not) is on my table header, I get the headings on a grey rectangle bar. I want to keep my headings but make the grey bar transparent.

IS this at all possible? If so, how?

Thanks
 
Ok, I've worked it out, though I'm not sure if it was the best way to do it. I did:

table.getTableHeader().setBackground(new Color(0,0,0,0));

 
Didn't test this, but what about

Code:
yourJTable.getJTableHeader().setBackground(Color.yourColor);
?

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top