TableCellRenderer keeps on Painting in JTable || Java Swing

By | August 20, 2018
Questions:

I am working on a little JTable. It currently looks like this JTable.
As you can see it keeps on painting with with the last color and I cant fix it for some reason.

    String[] columnNames = { "Spieler", "Verfügbare Truppen", "besetzte Länder" };

    Object[][] data = { { Player1.getName(), Player1.troopscore, Player1.countryscore },
            { Player2.getName(), Player2.troopscore, Player2.countryscore },
            { Player3.getName(), Player3.troopscore, Player3.countryscore },
            { Player4.getName(), Player4.troopscore, Player4.countryscore },
            { Player5.getName(), Player5.troopscore, Player5.countryscore },
            { Player6.getName(), Player6.troopscore, Player6.countryscore },

    };

    table = new JTable(data, columnNames) {

        @Override
        public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
            Component comp = super.prepareRenderer(renderer, row, col);

            for (int i = 0; i < playerlist.size(); i++) {
                Object value = getModel().getValueAt(i, 0);

                {

                    if (row == i && col == 0 && value == playerlist.get(i).getName()) {

                        {
                            System.out.println(value);
                            comp.setBackground(playerlist.get(i).player_color);
                        }

                    }

                }
            }

            return comp;

        }
    };

I would appreciate your help 🙂

Answers:

Leave a Reply

Your email address will not be published. Required fields are marked *