简体   繁体   English

根据数据库的值改变 JTable 行的颜色

[英]Change Color of JTable Row Based on the Value of the Database

tblApplicant = new javax.swing.JTable(){
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
    {
    Component c = super.prepareRenderer(renderer, row, column);

    //  Alternate row color
    String value = (String) tblApplicant.getValueAt(row, 4);
    if (value == "Single" && !isRowSelected(row))
    c.setBackground(Color.LIGHT_GRAY);

    return c;
}

}; };

This is my New Codes im trying to get the value of column 4 and equal it to single if its true the background is change.这是我的新代码,我试图获取第 4 列的值,如果背景发生变化,则它等于单值。 but this is not working但这不起作用

Overriding the prepareRender(...) method of the JTable allows your to customize rendering for the entire row based on a value in one of the columns.覆盖JTableprepareRender(...)方法允许您根据其中一列中的值自定义整行的呈现。

The basic logic would be something like:基本逻辑类似于:

public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
{
    Component c = super.prepareRenderer(renderer, row, column);

    //  Color row based on a cell value

    if (!isRowSelected(row))
    {
        c.setBackground(getBackground()); // set default background

        int modelRow = convertRowIndexToModel(row);
        String value = (String)getModel().getValueAt(modelRow, ???);

        if ("Single".equals(value)) c.setBackground(Color.GREEN);
    }

    return c;
}

Check outTable Row Rendering for more information and working examples.查看表格行渲染以获取更多信息和工作示例。

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

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