Link to home
Start Free TrialLog in
Avatar of pleasure
pleasure

asked on

Changing Table Header's Color

Can i change the JTable header's color? if yes, canu show me how??

thanks
Avatar of Mick Barry
Mick Barry
Flag of Australia image

The same way you change the colour of a table cell, by supplying a custom table cell renderer.

table.getColoumn(n).setHeaderRenderer(myrenderer);

or

table.getTableHeader().setDefaultRenderer(myrenderer);

Avatar of pleasure
pleasure

ASKER

hi objetcs,
thanks for the response... do u have any example of this program? or may be do u have any links related to this matter??

ASKER CERTIFIED SOLUTION
Avatar of bapi
bapi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
it's works... thanks you very much for the sample...:D
Any thing more apart from header's color?
ya..there is little problem... when i changed the header color, the separator between column at header became invisible.. is there any wat to make it better??
yes you can put a border for the label that has been used to render the column header...want the code?
Here the Code: Just Just change the HeaderRenderer.java

HeaderRendere.java
--------------------

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.plaf.basic.*;


public class HeaderRenderer extends JLabel implements TableCellRenderer{

  private Color fgColor = UIManager.getColor("TableHeader.foreground");
  private Color bgColor = UIManager.getColor("TableHeader.background");
  private javax.swing.border.Border border = UIManager.getBorder("TableHeader.cellBorder");

 public HeaderRenderer() {
    setOpaque(true);
 }

// Sets the foreground color for header
public void setForegroundColor(Color fgColor) {
  this.fgColor=fgColor;
}
// Gets the foreground color of header
public Color getForegroundColor() {
  return fgColor;
}
// Sets the background color for header
public void setBackgroundColor(Color bgColor) {
  this.bgColor=bgColor;
}
// Gets the background color of header
public Color getBackgroundColor() {
  return bgColor;
}
// Sets the border for header
public void setBorder(javax.swing.border.Border border) {
this.border = border;
}
// Gets the header border
public javax.swing.border.Border getBorder() {
     return border;
}

public Component getTableCellRendererComponent(JTable table, Object value,
                        boolean isSelected, boolean hasFocus, int row, int column) {
               JLabel label = this;
               label.setText((value ==null) ? "" : (" "+value.toString()));
               label.setForeground(getForegroundColor());
              label.setBackground(getBackgroundColor());
              label.setBorder(getBorder());
                  return label;
       }
}
thanks bapi..it's works great...:D