Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

java swing jtable different color each row

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;

public class MyTable extends JTable {
    public MyTable(TableModel model) {
        super(model);
    }

    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component c = super.prepareRenderer(renderer, row, column);
        if (!c.getBackground().equals(getSelectionBackground())) {
            Color colour = (row % 2 == 0 ? Color.WHITE : Color.LIGHT_GRAY);
            c.setBackground(colour);
        }
        return c;
    }
}
 
PREVIOUS NEXT
Tagged: #java #swing #jtable #color #row
ADD COMMENT
Topic
Name
3+6 =