w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Invoke prepareRenderer of JTable Swing

Problem solved by add to ProfitAbilityTableModel this methods:

public void setData(List<Client> clients){
        this.clients=clients;
    }

    public Client getClient(int index){
        return clients.get(index);
    }

Change my table:

public class MyTable extends JTable {

    public MyTable(TableModel model){
        super(model);   
    }
    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int
rowIndex, int vColIndex){
        Component rComp=super.prepareRenderer(renderer, rowIndex,
vColIndex);
        if(getModel()!=null){
            Client
client=((ProfitAbilityTableModel)getModel()).getClient(rowIndex);
            if(client.getExpected()==client.getReceived())
                rComp.setBackground(new Color(139, 255, 182));
            else
                rComp.setBackground(new Color(255,139,147));
        }

        return rComp;
    }
}

In main frame update data by button like this:

ProfitAbilityTableModel
model=(ProfitAbilityTableModel)table.getModel();
model.setData(ClientFactory.getClients(10));
model.fireTableDataChanged();




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.