customizing jtable cellrenderer with table's cell header color

℡╲_俬逩灬. 提交于 2019-11-27 07:10:58

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/6644739/customizing-jtable-cellrenderer-with-tables-cell-header-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!