简体   繁体   中英

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

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. This is error prone, hard to read and hard to maintain.

Instead you should use a 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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