[英]Change the color of a row in a jTable
I have a jTable as following : 我有一个jTable如下:
I want when the value in the Quantité
is less than the value in the Min seuil de suantité
, to change the color of the row to pink. 我希望当值
Quantité
小于该值Min seuil de suantité
,更改该行以粉红色的颜色。
In the load of the program all works fine, but when I do some event like click on the table, the color of all the rows is changed even if the the value of the Quantité
is not less than the value of the Min seuil de quantité
: 在程序的负载一切工作正常,但是当我做一些事情像点击桌子上,所有行的颜色发生改变,即使的价值
Quantité
不小于的值Min seuil de quantité
:
this is my cell rendering : 这是我的单元格渲染:
public class CustomTableCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table,
Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(
table, obj, isSelected, hasFocus, row, column);
setHorizontalAlignment(SwingConstants.LEFT);
int selectedRow = table.convertRowIndexToModel(row);
if (table.getModel().getValueAt(selectedRow, 3) != null && table.getModel().getValueAt(selectedRow, 4) != null) {
int quantite = Integer.parseInt(table.getModel().getValueAt(selectedRow, 3).toString());
int minQuantite = Integer.parseInt(table.getModel().getValueAt(selectedRow, 4).toString());
if (quantite < minQuantite) {
if (isSelected) {
cell.setBackground(new Color(255, 138, 239));
} else {
cell.setBackground(new Color(252, 189, 252));
}
}
}
return cell;
}
}
and this is the code which allows me to affect the cell rendering to my table : 这是使我能够影响到表格的单元格渲染的代码:
private void cellRendering(){
for (int i = 0; i < masterTable.getColumnCount(); i++) {
tcol = masterTable.getColumnModel().getColumn(i);
tcol.setCellRenderer(new CustomTableCellRenderer());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.