Render alternate color rows for a JTable - Java Swing

Java examples for Swing:JTable Row

Description

Render alternate color rows for a JTable

Demo Code


import java.awt.Color;
import java.awt.Component;
import java.text.NumberFormat;
import java.util.regex.Pattern;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

public class Main{
    /**/*from ww w  .j a  v  a  2s.  c o  m*/
     * Render alternate color rows for a JTable
     * 
     * @param table
     * @param colorsToRenderer
     */
    public static void renderAlternateColorTable(JTable table,
            Color[] colorsToRenderer) {
        int columnCount = table.getColumnCount();
        AlternateRowColorTableCellRenderer renderer = new AlternateRowColorTableCellRenderer(
                colorsToRenderer);
        for (int colIndex = 0; colIndex < columnCount; colIndex++) {
            TableColumn tc = table.getColumnModel().getColumn(colIndex);
            tc.setCellRenderer(renderer);
        }
        table.revalidate();
        table.repaint();
    }
}

Related Tutorials