简体   繁体   中英

How to change the color of the line depending on the condition?

My table looks like this, everything works and everything is OK) I need that if the condition ARRIVAL = 0 or DEPART = 0 is met, then the text color in the whole row was of a different color, for example, red or black, it does not matter, I can not figure out how to change it, maybe it's easy, I'm new) I haven't found an answer to this question in other questions

 private void setTableSettingsReport() {

    jTblReport.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jTblReport.setRowSelectionAllowed(true);
    jTblReport.setAutoCreateRowSorter(true);
    jTblReport.getTableHeader().setReorderingAllowed(false);

    String[] dbColNames = new String[13];
    dbColNames[0] = "ID";
    dbColNames[1] = "Имя";
    dbColNames[2] = "Фамилия";
    dbColNames[3] = "Отчество";
    dbColNames[4] = "Дата прихода с ";
    dbColNames[5] = "Дата прихода до";
    dbColNames[6] = "Прибытие";
    dbColNames[7] = "Дата ухода с ";
    dbColNames[8] = "Дата ухода до";
    dbColNames[9] = "Убытие";
    dbColNames[10] = "Причина опоздания";
    dbColNames[11] = "Причина раннего ухода";
    dbColNames[12] = "ID лица";

    // dbColNames[8] = "Дата начала";
    //  dbColNames[9] = "Дата окончания";    
    tm.setColumnIdentifiers(dbColNames);
    jTblReport.setModel(tm);
    jTblReport.setSelectionForeground(Color.white);

    jTblReport.setSelectionBackground(Color.red);
    jTblReport.getColumnModel().getColumn(0).setPreferredWidth(10);
    jTblReport.getColumnModel().getColumn(1).setPreferredWidth(60);
    jTblReport.getColumnModel().getColumn(2).setPreferredWidth(60);
    jTblReport.getColumnModel().getColumn(3).setPreferredWidth(60);
    jTblReport.getColumnModel().getColumn(4).setPreferredWidth(60);
    jTblReport.getColumnModel().getColumn(5).setPreferredWidth(60);
    jTblReport.getColumnModel().getColumn(6).setPreferredWidth(60);
    jTblReport.getColumnModel().getColumn(7).setPreferredWidth(60);
    jTblReport.getColumnModel().getColumn(8).setPreferredWidth(60);
    jTblReport.getColumnModel().getColumn(9).setPreferredWidth(60);
    jTblReport.getColumnModel().getColumn(10).setPreferredWidth(60);
    jTblReport.getColumnModel().getColumn(10).setPreferredWidth(60);
    jTblReport.getColumnModel().getColumn(10).setPreferredWidth(60);

}

public void searchIdentificationsReport(boolean all) {
    int z = 0;
    while (z < tm.getRowCount()) {
        tm.removeRow(z);
    }
    Statement statement = null;
    try {
        statement = getDbConnection().createStatement();
        String sql = "select a.id, pr.p_name,pr.p_surname,pr.p_patronic, a.date_arrival_from,a.date_arrival_to, a.arrival,a.date_departure_from,a.date_departure_to, a.depart, a.arrival_comment, a.depart_comment,a.prsn_id "
                + "  FROM bio.persons pr, attendance a where pr.p_id=a.prsn_id and a.date<=CURDATE() ";
        if (cbPersons.getSelectedItem() != null && model.getSelectedItem() != null) {
            CodeValueDTO dto = (CodeValueDTO) model.getSelectedItem();
            sql += " and  pr.p_id='" + dto.getId() + "'";
        }
        if (!all) {
            sql += " and (ARRIVAL =0 or DEPART=0)";


        }

        statement.execute(sql);
        ResultSet rs = statement.getResultSet();


        while (rs.next()) {
            Object[] objects = new Object[13];
            for (int i = 0; i < 13; i++) {
                objects[i] = rs.getObject(i + 1);
            }
            if (rs.getInt(7) == 0) {
                objects[6] = "Нет";
            } else {
                objects[6] = "Да";
            }

            if (rs.getInt(10) == 0) {
                objects[9] = "Нет";
            } else {
                objects[9] = "Да";
            }

            tm.addRow(objects);
        }

    } catch (SQLException ex) {
        ex.printStackTrace();
    } finally {
        try {
            statement.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

}

dbColNames[6] = "Прибытие"; dbColNames[9] = "Убытие"; these are the same fields depending on which you need to change the color of the entire line

You need to set a own cell-renderer for this JTable (I assume you are using a JTable).

public class MyCellRenderer extends DefaultTableCellRenderer{
private int columNum = 0;

public MyCellRenderer(int columNum) {
    // TODO Auto-generated constructor stub
    this.columNum = columNum;
}

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    // TODO Auto-generated method stub
    Object object = table.getValueAt(row, this.columNum);

    if(object.equals("yourValue")) {
        setBackground(Color.RED);
    }
    return this;
}

}

And you need to set this renderer to your jtable right after creating it.

jTabReport.setDefaultRenderer(Object.class, new MyCellRenderer(3));

For that you create a class like this and extend from DefaultTableCellRenderer and overwrite the getTableCellRendererComponent. The constructor gets passed the columnumber where youre "arival" information is stored. For each cell in the row you check if the specified colum contains arival = 0 or not (This must be always the same colum). If so set the background. "yourValue" is just an example.

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