Java JTable Header setTableHeaderCellRenderer(TableColumn tableCol)

Here you can find the source of setTableHeaderCellRenderer(TableColumn tableCol)

Description

set Table Header Cell Renderer

License

Open Source License

Declaration

public static void setTableHeaderCellRenderer(TableColumn tableCol) 

Method Source Code

//package com.java2s;
/**//from ww w  . j  a  va  2  s  .  co m
 * JTableHelper
 *
 * Created on 03.11.2008, 20:58:32
 *
 *  Copyright (C) 03.11.2008  <reiner>
 *
 * @author reiner
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

import java.awt.Component;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;

import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class Main {
    public static void setTableHeaderCellRenderer(TableColumn tableCol) {
        tableCol.setHeaderRenderer(new DefaultTableCellRenderer() {
            public Component getTableCellRendererComponent(JTable table,
                    Object value, boolean isSelected, boolean hasFocus,
                    int row, int column) {
                if (table != null) {
                    JComponent c = (JComponent) table.getTableHeader()
                            .getDefaultRenderer();
                    setForeground(c.getForeground());
                    setBackground(c.getBackground());
                    setFont(c.getFont());
                    setBorder(UIManager.getBorder("TableHeader.cellBorder"));
                    // setBorder(c.getBorder());
                }
                setText(value != null ? value.toString() : "");
                // setBorder(UIManager.getBorder("TableHeader.cellBorder"));
                setHorizontalAlignment(JLabel.CENTER);
                return this;
            }
        });
    }

    public static void setTableHeaderCellRenderer(JTable table) {
        TableColumnModel colModel = table.getColumnModel();
        int i, anzahl = colModel.getColumnCount();
        for (i = 0; i < anzahl; i++)
            setTableHeaderCellRenderer(colModel.getColumn(i));
    }
}

Related

  1. paintHeader(Graphics graphics, JTable table, int x, int width)
  2. removeBadChars(JTable table, boolean skipHeaderBoolean)
  3. setHeaderIcon(JTable table, final int column, final ImageIcon icon)
  4. setOptimalHeaderWidth(int col)
  5. setPressedColumn(JTableHeader tableHeader, int columnModelIndex)
  6. setupAsRowHeader(final JTable table)
  7. sizeColumnsToFit(JTable table, boolean useHeader, boolean useContent)
  8. tableHeaders(JTable jtable)