Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

java swing jtable zebra stripes

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 #zebra #stripes
ADD COMMENT
Topic
Name
4+1 =