Search code examples
javaswingjtablenimbus

Change default row colors in jtable


I'm developing a simple java application using swing. I use JTable element. The problem is that by default rows of tables are white and grey like in this post Setting color in a row of a Jtable . I want to make them the same color, for example all rows white.


Solution

  • You can override the prepareRenderer method of JTable like this

    JTable table = new JTable(...)
    {
        public Component prepareRenderer(
            TableCellRenderer renderer, int row, int column)
        {
            Component c = super.prepareRenderer(renderer, row, column);
            c.setBackground(Color.WHITE);
            return c;
        }
    };
    

    Or you could create your own TableCellRenderer which does the same thing (picking the background color to render) but on a Cell level and use that renderer for each of your columns.