Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to set Background color of tableHeader

843806Feb 3 2009 — edited Feb 4 2009
Hi

I have search mach forums but i am not finding the solution or the problem like mine.
I have Custom JTable with custom cell renderer. I try every thing to setBackground to TableHeader but it's not working
My L&F was set to this and it works
 UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
but after that when i change it to
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
it's not working.
May be it uses the com.sun.java.swing.plaf.windows.WindowsTableHeaderUI$XPDefaultRenderer for renderer, and i think it paints the header with windows color and fonts.
Below are my table and custom renderer. Please help :( :( :(
private void init() {
        UIManager.put("Table.sortIconColor", UIConstants.BACKGROUND_TITLE);
        UIManager.put("Table.background", ColorUIResource.WHITE);
        UIManager.put("Table.foreground", UIConstants.FORGRAOUND_LABEL);
        UIManager.put("TableHeader.background", UIConstants.BACKGROUND_TITLE);
        UIManager.put("TableHeader.foreground", UIConstants.FORGRAOUND_TITLE);
        UIManager.put("Table.scrollPaneBorder", ColorUIResource.WHITE);


        this.setDragEnabled(false);
        this.setAutoscrolls(true);
        this.setCellSelectionEnabled(false);
        this.setRowSelectionAllowed(true);
        this.setFocusable(true);
        this.setSelectionBackground(ColorUIResource.WHITE);
        this.customRenderer = new ColorTableRenderer();
        this.setDefaultRenderer(Object.class, customRenderer);
        this.setDefaultRenderer(Integer.class, customRenderer);
//        this.getTableHeader().setDefaultRenderer(customRenderer);

    }
  
public class ColorTableRenderer extends DefaultTableCellRenderer {

        public Component getTableCellRendererComponent(
                JTable table,
                java.lang.Object value,
                boolean isSelected,
                boolean hasFocus,
                int row, int column) {
            if (!isSelected) {
                Color c = table.getBackground();
                if ((row % 2) == 0 &&
                        c.getRed() > 10
                        && c.getGreen() > 10
                        && c.getBlue() > 10) {
                    setBackground(new Color(c.getRed() - 10,
                            c.getGreen() - 10,
                            c.getBlue() - 10));
                } else {
                    setBackground(c);
                }
            }
            return super.getTableCellRendererComponent(table,
                    value, isSelected, hasFocus, row, column);
        }

        public void setValue(Object value) {
            if (value instanceof ColorData) {
                ColorData cvalue = (ColorData) value;
                super.setForeground(cvalue.color);
                super.setValue(cvalue.data.toString());
            } else {
                super.setValue(value);
                setForeground(Color.BLACK);
            }
        }
    }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 4 2009
Added on Feb 3 2009
1 comment
731 views