Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

swing - customizing jtable cellrenderer with table's cell header color

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...