简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM