javaswingjtablecellrenderer

How to change a cell backgound of JTable based on previous cell value?


I'm trying to change the background of a JTable cells, based on the value of the cell of the column -1.

So if a value of cell at[row, Column] is diffrent from given values eg "A", "B", then change the background of the given cell and the cell at [row, column+1]

Here is what I get now:

enter image description here

What I want is the following :

enter image description here

I used a custom cell rendered, but I can't achieve this requirements.

This is my code:

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class TestCellRendered {

    private JFrame frame;
    private JTable table;

    private DefaultTableModel model;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestCellRendered window = new TestCellRendered();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     * @throws IOException 
     */
    public TestCellRendered() throws IOException {
        initialize();

    }



    /**
     * Initialize the contents of the frame.
     * @throws IOException 
     */
    private void initialize() throws IOException {
        frame = new JFrame();
        frame.setResizable(false);
        frame.setBounds(100, 100, 975, 756);
        frame.getContentPane().setLayout(null);

        model = new DefaultTableModel();
        model.addColumn("À remplacer");
        model.addColumn("Lignes");
        table = new JTable(model);
        MyRenderer myRenderer = new MyRenderer();   // See below
        table.setDefaultRenderer(Object.class   , myRenderer);
        table.setBackground(Color.LIGHT_GRAY);
        Font font = new Font("",1,14);
        table.setForeground(Color.black);
        table.setFont(font);
        //Add rows to my table
        model.addRow(new Object[]{"a",""});
        model.addRow(new Object[]{"b", ""});
        model.addRow(new Object[]{"c", ""});


        JScrollPane scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setBounds(66, 66, 519, 421);
        frame.getContentPane().add(scrollPane);


    }
    class MyRenderer extends DefaultTableCellRenderer  
    { 
        public Component getTableCellRendererComponent(JTable table, Object value, boolean   isSelected, boolean hasFocus, int row, int column) 
    { 
        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 

        if (! table.isRowSelected(row))
        {
            if( !(value.equals("a") |value.equals("b") ))
                {
                c.setBackground(new java.awt.Color(231, 76, 60));
                c.setForeground(new  java.awt.Color(255,255,255));
                }
            else{
                c.setBackground(table.getBackground());
                c.setForeground(table.getForeground());
            }
        }


        return c; 
    } 

    } 
}

Solution

  • based on the value of the cell of the column -1.

    if( !(value.equals("a") |value.equals("b") ))
    

    Well you are always doing the test on the current column. You need to do the test on the first column:

    String text = table.getValueAt(row, 0).toString();
    
    if( !(text.equals("a") |text.equals("b") ))
    

    Note it is usually easier to write code in a if statement with a positive test:

    if (text.equals("a" || text.equals("b"))
    {
        c.setBackground(table.getBackground());
        c.setForeground(table.getForeground());
    }
    else
    {
        c.setBackground(new java.awt.Color(231, 76, 60));
        c.setForeground(new  java.awt.Color(255,255,255));
    }