简体   繁体   English

更改jTable中行的颜色

[英]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());
        }
    }

The renderer is a rubber stamp that remembers what ink was applied last. 渲染器是一个橡皮图章,可以记住上一次施加的墨水。 Be sure to set the desired color each time the renderer is invoked. 确保每次调用渲染器时都设置所需的颜色。 More details can be found here . 可以在此处找到更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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