简体   繁体   English

更改从jar文件获取的jtable中特定行的颜色

[英]Change color of a specific row in jtable that is fetched from jar file

I want to change color of a specific row in jtable that is fetched from jar file 我想更改从jar文件获取的jtable中特定行的颜色

Statement stmt = myConn.createStatement();

String sqlQuery = ("Select * from ATTENDENCE where Class='"+c+"' AND Section='"+s+"' AND Date='"+date+"';");

ResultSet result = stmt.executeQuery(sqlQuery);
while(result.next())
{
    String ad = result.getString("Status");      
    if (ad.equalsIgnoreCase("absent"))
    {
        setForeground(Color.red);
    }
    table_1.setModel(DbUtils.resultSetToTableModel(result));
}

You need to implement a renderer and apply it. 您需要实现一个渲染器并应用它。

table.getColumnModel().getColumn(0).setCellRenderer(new CustomRenderer());

The class renderer should be like that (for example): 类渲染器应该是这样的(例如):

class CustomRenderer extends DefaultTableCellRenderer 
{
private static final long serialVersionUID = 6703872492730589499L;

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        if(row == 0){
            cellComponent.setBackground(Color.YELLOW);
        } else if ( row == 1){
            cellComponent.setBackground(Color.GRAY);
        } else {
            cellComponent.setBackground(Color.CYAN);
        }
        return cellComponent;
    }
}
String sqlQuery = ("Select * from ATTENDENCE where Class='"+c+"' AND Section='"+s+"' AND Date='"+date+"';");

Don't use string concatenation to build your SQL query. 不要使用字符串连接来构建SQL查询。 This is error prone, hard to read and hard to maintain. 这容易出错,难以阅读且难以维护。

Instead you should use a PreparedStatement . 相反,您应该使用PreparedStatement This allows you to specify multiple tokens which can then be replaced by the value of your variable: 这允许您指定多个标记,然后可以将其替换为变量的值:

String sql = "Select * from Attendance where Class = ? and Section = ? and Date = ?";
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString(1, c);
...
stmt.setDate(3, date);
ResultSet rs = stmt.executeQuery();

I want to change color of a specific row 我想更改特定行的颜色

Looks like you want to color a row based on a value in the row. 看起来您想根据行中的值为行着色。

Rendering is done when the table is painted, not when you get the data from the database. 绘制是在绘制表时完成的,而不是从数据库中获取数据时完成的。

One way to do this is to override the prepareRenderer(...) method of the JTable. 一种方法是重写JTable的prepareRenderer(...)方法。 Something like: 就像是:

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

        if (!isRowSelected(row))
        {
            c.setBackground(getBackground());
            int modelRow = convertRowIndexToModel(row);
            String status = (String)getModel().getValueAt(modelRow, ???);
            if ("absent".equals(status)) c.setBackground(Color.RED);
        }

        return c;
    }
};

Check out Table Row Rendering for more information and working examples to get you started. 查看Table Row Rendering了解更多信息和工作示例,以帮助您入门。

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

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