Java JTable Cell Renderer setTableRenderer(JTable table, Integer alignment)

Here you can find the source of setTableRenderer(JTable table, Integer alignment)

Description

This method changes the default renderer of a given table

License

Open Source License

Parameter

Parameter Description
table - the table where the renderer will be applied
alignment - the alignment for the cells

Declaration

public static void setTableRenderer(JTable table, Integer alignment) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;
import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class Main {
    /**This method changes the default renderer of a given table
     * @param table - the table where the renderer will be applied 
     * @param alignment - the alignment for the cells*/
    public static void setTableRenderer(JTable table, Integer alignment) {

        final Integer alnt = alignment;

        table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {

            private static final long serialVersionUID = -8183629931912150659L;

            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                    boolean hasFocus, int row, int column) {
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                if (row % 2 != 0) {
                    setBackground(Color.decode("#F0F0F0"));
                } else {
                    setBackground(null);
                }/*from ww w .  ja  va2  s.  c  o  m*/

                if (alnt != null)
                    this.setHorizontalAlignment(alnt);
                return this;
            }
        });
    }

    /**This method changes the default renderer of a given table
     * @param table - the table where the renderer will be applied 
     * @param indexs - type of renderer for each index
     * @param colors - color of renderer each type in the indexs vector
     * @param alignment - the alignment for the cells*/
    public static void setTableRenderer(JTable table, int[] indexs, Color[] colors, Integer alignment) {

        final Integer alnt = alignment;
        final Color[] clrs = colors;
        final int[] idxs = indexs;

        table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {

            private static final long serialVersionUID = -8183629931912150659L;

            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                    boolean hasFocus, int row, int column) {
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

                if (idxs[row] >= clrs.length) {
                    if (row % 2 != 0)
                        setBackground(Color.decode("#F0F0F0"));
                    else
                        setBackground(null);
                } else
                    setBackground(clrs[idxs[row]]);

                if (alnt != null)
                    this.setHorizontalAlignment(alnt);
                return this;
            }
        });
    }
}

Related

  1. createDefaultTableCellRenderer()
  2. getCellRenderer(final int hMargin)
  3. getDefaultRenderer(JTable table, TableColumn column)
  4. getTableCellRendererClass( Class propType)
  5. setCellRender(JTable table, int columnIdx, TableCellRenderer render)
  6. setUpCellRenderer(JTable table)