customizing jtable cellrenderer with table's cell header color
Asked Answered
D

1

3

That's a question really similar to this previous post of mine. I need to customize some cell of a JTable, in a way that they would look like a table header cell. I'm using Nimbus look and feel and I'm trying to retrieve the color of JTable's editor:

public class HeaderCellRenderer extends JLabel implements TableCellRenderer{


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

        System.out.println("OK");
        this.setOpaque(true);
        setBackground(UIManager.getColor("TableHeader.background"));
        return this;
    }

}

I follow this post to get the name to be supplied to getColor method ("TableHeader.background"). Despite what I've done since now, the color returned isn't the same of my table's header cells.

Do you have any idea?

EDIT:

I noticed that instead of a color it should be a gradient but I can't find the right key to use. I looked this list.

Donnydonnybrook answered 11/7, 2011 at 0:45 Comment(0)
S
3

The appearance of the default table header for a typical Look & Feel is provided by an instance of sun.swing.table.DefaultTableCellHeaderRenderer. You can obtain a copy as follows:

class HeaderRenderer implements TableCellRenderer {

    TableCellRenderer renderer;

    public HeaderRenderer(JTable table) {
        renderer = table.getTableHeader().getDefaultRenderer();
    }

    @Override
    public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected,
        boolean hasFocus, int row, int col) {
        return renderer.getTableCellRendererComponent(
            table, value, isSelected, hasFocus, row, col);
    }
}

and you can install it in the usual way for a given column's type token:

table.setDefaultRenderer(SomeObject.class, new HeaderRenderer(table));
Sticktight answered 11/7, 2011 at 1:34 Comment(1)
See also Darryl Burke's Default Table Header Cell Renderer.Sticktight

© 2022 - 2024 — McMap. All rights reserved.