Here you can find the source of setTableRenderer(JTable table, Integer alignment)
Parameter | Description |
---|---|
table | - the table where the renderer will be applied |
alignment | - the alignment for the cells |
public static void setTableRenderer(JTable table, Integer alignment)
//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; } }); } }