customizing jtable cellrenderer with table's cell header color

前端 未结 1 738
感情败类
感情败类 2020-12-11 09:00

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 usin

相关标签:
1条回答
  • 2020-12-11 09:54

    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));
    
    0 讨论(0)
提交回复
热议问题