使用modify prepareRenderer实现JTable
。
public class MyTable extends JTable {
private List<Client> list;
public MyTable(TableModel model){
super(model);
if (model instanceof ProfitAbilityTableModel){
list=((ProfitAbilityTableModel)model).getClients();
}
}
@Override
public Component prepareRenderer(TableCellRenderer renderer, int rowIndex, int vColIndex){
Component rComp=super.prepareRenderer(renderer, rowIndex, vColIndex);
if(list!=null){
Client client=list.get(rowIndex);
if(client.getExpected()==client.getReceived())
rComp.setBackground(new Color(139, 255, 182));
else
rComp.setBackground(new Color(255,139,147));
}
return rComp;
}
}
ProfitAbilityTableModel
public class ProfitAbilityTableModel extends AbstractTableModel{
private String []columnNames={"Имя учетной записи", "Ф.И.О. Клиента","Адрес",
"Получено грн.", "Предпологалось грн."};
private List<Client> clients;
public ProfitAbilityTableModel(List<Client> clients){
this.clients=clients;
}
public ProfitAbilityTableModel(){};
@Override
public int getColumnCount() {
return 5;
}
@Override
public int getRowCount() {
return (clients!=null) ? clients.size(): 0;
}
@Override
public Object getValueAt(int r, int c) {
Client client=clients.get(r);
switch (c) {
case 0:
return client.getUserName();
case 1:
return client.getName() +" " + client.getSurname() + " " + client.getPatronymic();
case 2:
return client.getAddress();
case 3:
return client.getReceived();
case 4:
return client.getExpected();
default:
return null;
}
}
@Override
public String getColumnName(int c){
return columnNames[c];
}
public List<Client> getClients() {
return clients;
}
}
在mainFrame中创建具有空tableModel的表 table = new MyTable(new ProfitAbilityTableModel());
按下按钮时执行此操作
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
table.setModel(new ProfitAbilityTableModel(ClientFactory.getClients(10)));
}
});
其中ClientFactory:
public class ClientFactory {
public static List<Client> getClients(int amount){
List<Client> result=new ArrayList<>();
Client c;
int received,expected;
for(int i=0;i<amount;i++){
c=new Client();
c.setUserName("username " + i);
c.setName("name "+ i);
c.setSurname("surname " + i);
c.setPatronymic("patronymic" + i);
c.setAddress("address " + i);
expected=(int)(Math.random()*100);
received=(int)(Math.random()*100);
if(received>expected)
received=expected;
c.setExpected(expected);
c.setReceived(received);
result.add(c);
}
return result;
}
}
如果Client
的两个字段相等,则将其设为行的更改颜色。
创建MyTable
时它工作正常,但是当更改tableModel时,所有行都有默认的背景颜色(白色)。如何在更改表模型时调用此代码?
答案 0 :(得分:0)
通过向ProfitAbilityTableModel添加此方法解决了问题:
public void setData(List<Client> clients){
this.clients=clients;
}
public Client getClient(int index){
return clients.get(index);
}
改变我的表格:
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;
}
}
在主框架中通过按钮更新数据,如下所示:
ProfitAbilityTableModel model=(ProfitAbilityTableModel)table.getModel();
model.setData(ClientFactory.getClients(10));
model.fireTableDataChanged();