Java Utililty Methods JTable Cell Renderer

List of utility methods to do JTable Cell Renderer

Description

The list of methods to do JTable Cell Renderer are organized into topic(s).

Method

voidadaptToTable(final JComponent renderer, final JTable table, boolean isSelected, final boolean hasFocus, final int row, final int column)
Adapts the given component to the specified table instance, making it look just as a DefaultTableCellRenderer would.
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;
...
voidapplyRenderers(final JTable table, final TableCellRenderer[] renderers)
apply Renderers
final TableColumnModel columnModel = table.getColumnModel();
for (int i = 0; i < renderers.length; i++) {
    final TableCellRenderer rend = renderers[i];
    if (rend != null) {
        columnModel.getColumn(i).setCellRenderer(rend);
voidapplyRenderers(final JTable table, final TableCellRenderer[] renderers)
apply Renderers
final TableColumnModel columnModel = table.getColumnModel();
for (int i = 0; i < renderers.length; i++) {
    final TableCellRenderer rend = renderers[i];
    if (rend != null) {
        columnModel.getColumn(i).setCellRenderer(rend);
voidconfigureColumn(JTable table, int columnNumber, Integer width, TableCellRenderer renderer, TableCellEditor editor)
configure Column
TableColumn column = table.getColumnModel().getColumn(columnNumber);
if (width != null)
    column.setMaxWidth(width);
if (renderer != null)
    column.setCellRenderer(renderer);
if (editor != null)
    column.setCellEditor(editor);
DefaultTableCellRenderercreateDefaultTableCellRenderer()
create Default Table Cell Renderer
DefaultTableCellRenderer label = new DefaultTableCellRenderer() {
    private static final long serialVersionUID = 1L;
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column) {
        if (table != null) {
            JTableHeader header = table.getTableHeader();
            if (header != null) {
                setForeground(header.getForeground());
...
DefaultTableCellRenderergetCellRenderer(final int hMargin)
get Cell Renderer
return new DefaultTableCellRenderer() {
    Border padding = BorderFactory.createEmptyBorder(0, hMargin, 0, hMargin);
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        setBorder(BorderFactory.createCompoundBorder(getBorder(), padding));
        setHorizontalAlignment(JLabel.RIGHT);
...
TableCellRenderergetDefaultRenderer(JTable table, TableColumn column)
Return the default cell renderer for the given JTable column.
try {
    return table.getTableHeader().getDefaultRenderer();
} catch (Exception e) {
    throw new RuntimeException(e);
ClassgetTableCellRendererClass(Class propType)
get Table Cell Renderer Class
return cellRendererClasses.get(propType);
voidsetCellRender(JTable table, int columnIdx, TableCellRenderer render)
set Cell Render
TableColumn o = table.getColumnModel().getColumn(columnIdx);
o.setCellRenderer(render);
voidsetTableRenderer(JTable table, Integer alignment)
This method changes the default renderer of a given table
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"));
...