Java JTable Cell Renderer adaptToTable(final JComponent renderer, final JTable table, boolean isSelected, final boolean hasFocus, final int row, final int column)

Here you can find the source of adaptToTable(final JComponent renderer, final JTable table, boolean isSelected, final boolean hasFocus, final int row, final int column)

Description

Adapts the given component to the specified table instance, making it look just as a DefaultTableCellRenderer would.

License

Apache License

Parameter

Parameter Description
renderer Your renderer component.
table Table this renderer is used by.
isSelected see TableCellRenderer.
hasFocus see TableCellRenderer.
row see TableCellRenderer.
column see TableCellRenderer.

Declaration

public static void adaptToTable(final JComponent renderer, final JTable table, boolean isSelected,
        final boolean hasFocus, final int row, final int column) 

Method Source Code

//package com.java2s;
/**//from  w  ww.  j ava 2s . c o  m
 * A small utility class which can be used to mimic the DefaultXXXRenderer behaviour for custom renderer
 * implementations. Basically, it offers methods for tables, lists and treetables, which expose the same signature as
 * the according renderer interface, with the addition of a JComponent. The specified component will be configured in
 * the same way as the default renderer implementation regardings fonts, borders and such.
 * <p/>
 * <hr/> Copyright 2006-2012 Torsten Heup
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

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

public class Main {
    /**
     * Adapts the given component to the specified table instance, making it look just as a DefaultTableCellRenderer
     * would.
     *
     * @param renderer   Your renderer component.
     * @param table      Table this renderer is used by.
     * @param isSelected see TableCellRenderer.
     * @param hasFocus   see TableCellRenderer.
     * @param row        see TableCellRenderer.
     * @param column     see TableCellRenderer.
     */
    public static void adaptToTable(final JComponent renderer, final JTable table, boolean isSelected,
            final boolean hasFocus, final int row, final int column) {
        Color fg = null;
        Color bg = null;

        JTable.DropLocation dropLocation = table.getDropLocation();
        if (dropLocation != null && !dropLocation.isInsertRow() && !dropLocation.isInsertColumn()
                && dropLocation.getRow() == row && dropLocation.getColumn() == column) {

            fg = UIManager.getColor("Table.dropCellForeground");
            bg = UIManager.getColor("Table.dropCellBackground");

            isSelected = true;
        }

        if (isSelected) {
            renderer.setForeground(fg == null ? table.getSelectionForeground() : fg);
            renderer.setBackground(bg == null ? table.getSelectionBackground() : bg);
        } else {
            renderer.setForeground(table.getForeground());
            renderer.setBackground(table.getBackground());
        }

        renderer.setFont(table.getFont());

        if (hasFocus) {
            Border border = null;
            if (isSelected) {
                border = UIManager.getBorder("Table.focusSelectedCellHighlightBorder");
            }
            if (border == null) {
                border = UIManager.getBorder("Table.focusCellHighlightBorder");
            }
            renderer.setBorder(border);

            if (!isSelected && table.isCellEditable(row, column)) {
                Color col;
                col = UIManager.getColor("Table.focusCellForeground");
                if (col != null) {
                    renderer.setForeground(col);
                }
                col = UIManager.getColor("Table.focusCellBackground");
                if (col != null) {
                    renderer.setBackground(col);
                }
            }
        } else
            renderer.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
    }
}

Related

  1. applyRenderers(final JTable table, final TableCellRenderer[] renderers)
  2. applyRenderers(final JTable table, final TableCellRenderer[] renderers)
  3. configureColumn(JTable table, int columnNumber, Integer width, TableCellRenderer renderer, TableCellEditor editor)
  4. createDefaultTableCellRenderer()