Java JTable Header setupAsRowHeader(final JTable table)

Here you can find the source of setupAsRowHeader(final JTable table)

Description

Setups the given table for usage as row-header.

License

Open Source License

Parameter

Parameter Description
table The table to setup as row headers.

Return

The renderer which has been assigned to the table.

Declaration

public static TableCellRenderer setupAsRowHeader(final JTable table) 

Method Source Code


//package com.java2s;
/*//from   ww w  . j a  v  a2 s  . c  o  m
 *    Geotoolkit.org - An Open Source Java GIS Toolkit
 *    http://www.geotoolkit.org
 *
 *    (C) 2001-2012, Open Source Geospatial Foundation (OSGeo)
 *    (C) 2009-2012, Geomatys
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library 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
 *    Lesser General Public License for more details.
 */

import java.awt.*;
import javax.swing.*;

import java.util.Locale;

import javax.swing.table.TableColumn;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.DefaultTableCellRenderer;

public class Main {
    /**
     * Setups the given table for usage as row-header. This method setups the background color to
     * the same one than the column headers.
     *
     * {@note In a previous version, we were assigning to the row headers the same cell renderer than
     *        the one created by <cite>Swing</cite> for the column headers. But it produced strange
     *        effects when the L&F uses a vertical grandiant instead than a uniform color.}
     *
     * @param  table The table to setup as row headers.
     * @return The renderer which has been assigned to the table.
     */
    public static TableCellRenderer setupAsRowHeader(final JTable table) {
        final JTableHeader header = table.getTableHeader();
        Color background = header.getBackground();
        Color foreground = header.getForeground();
        if (background == null || background.equals(table.getBackground())) {
            if (!SystemColor.control.equals(background)) {
                background = SystemColor.control;
                foreground = SystemColor.controlText;
            } else {
                final Locale locale = table.getLocale();
                background = UIManager.getColor("Label.background", locale);
                foreground = UIManager.getColor("Label.foreground", locale);
            }
        }
        final DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setBackground(background);
        renderer.setForeground(foreground);
        renderer.setHorizontalAlignment(DefaultTableCellRenderer.RIGHT);
        final TableColumn column = table.getColumnModel().getColumn(0);
        column.setCellRenderer(renderer);
        column.setPreferredWidth(60);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setCellSelectionEnabled(false);
        return renderer;
    }
}

Related

  1. removeBadChars(JTable table, boolean skipHeaderBoolean)
  2. setHeaderIcon(JTable table, final int column, final ImageIcon icon)
  3. setOptimalHeaderWidth(int col)
  4. setPressedColumn(JTableHeader tableHeader, int columnModelIndex)
  5. setTableHeaderCellRenderer(TableColumn tableCol)
  6. sizeColumnsToFit(JTable table, boolean useHeader, boolean useContent)
  7. tableHeaders(JTable jtable)