7

I'm trying to write a method which for given parameters (value, color), sets color on the background of a cell which has value equal to cellValue.

What my method actually does is, it sets color on the background of a cells for whole row and when I select the row on the table, and I want method to only set color at specific column (where cellValue is equal to value) each time I call the method.

    void setCellBackgroundColor(boolean cellValue, final Color color) {
        List<List<Object>> data = tView.getTTableModel().getData();

        for (int row = 0; row < data.size(); row++) {
            for (int col = 0; col < data.get(row).size(); col++) {
                TableCellRenderer renderer = tView.table.getCellRenderer(row, Col);
                Component component = tView.table.prepareRenderer(renderer, row, col);
                boolean bValue = 
                    TDataTypeRenderer.parseIntoRealValue(
                        data.get(row).get(col)
                    )
                );
                if (bValue == cellValue) {
                    component.setBackground(color);
                }
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Xerath
  • 1,069
  • 5
  • 17
  • 26
  • and prepareRenderer is good and proper of possible ways (has column and rows coordinates), but is part of override JTables methods not to loop inside row & columns, result renderer (prepareRenderer) should be only one instance that loops inside view, note prepareRenderer hates empty JTables view, have to test if rows > 0 before call(s) is/are painted – mKorbel Aug 01 '14 at 13:19

1 Answers1

11

when I select the row on the table, and I want method to only set color at specific column

Try with overridden prepareRenderer() method as suggested by @mKorbel.

sample code:

Object[] columnNames = { "A", "B", "C", "D" };
Object[][] data = { 
        { "abc", new Double(850.503), 53, true },
        { "lmn", new Double(36.23254), 6, false }, 
        { "pqr", new Double(8.3), 7, false },
        { "xyz", new Double(246.0943), 23, true } };

JTable table = new JTable(data, columnNames) {
    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
        Component comp = super.prepareRenderer(renderer, row, col);
        Object value = getModel().getValueAt(row, col);
        if (getSelectedRow() == row) {
            if (value.equals(false)) {
                comp.setBackground(Color.red);
            } else if (value.equals(true)) {
                comp.setBackground(Color.green);
            } else {
                comp.setBackground(Color.white);
            }
        } else {
            comp.setBackground(Color.white);
        }
        return comp;
    }
};

When selected first row:

enter image description here

When selected second row.

enter image description here

Read more...


EDIT

As per your last comment

Is it possible to change color with out clicking (selecting) row on the table?

Yes just remove the check of selected row.

    Object value = getModel().getValueAt(row, col);
    if (value.equals(false)) {
        comp.setBackground(Color.red);
    } else if (value.equals(true)) {
        comp.setBackground(Color.green);
    } else {
       comp.setBackground(Color.white);
    }

enter image description here

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • 2
    prepareRenderer is good and proper way, OP issue that her/his code loops inside JTables view and to add bunch of renderers – mKorbel Aug 01 '14 at 13:22
  • Thank you guys, now I understand where is the problem. – Xerath Aug 01 '14 at 13:32
  • 2
    @Xerath I have update my code to select only cells of selected row. – Braj Aug 01 '14 at 13:35
  • Hey again, but this is only selecting when I click on the table. Is it possible to change color with out clicking (selecting) row on the table ? – Xerath Aug 01 '14 at 14:19
  • Can anybody tell me how to use this with the Netbeans GUI Builder? I know how to use Customize code. But can't seem to work it out. – m4heshd Nov 28 '16 at 08:35