Search code examples
javaswingjtable

JTable Change Cell Background Color from DataModel


This may sound strange, but the best way to explain my problem is just as stated in the title.

I Have a JTable, that uses a Custom Tablemodel, in this Tablemodel i have different methods.

I need a way for when I call a specific method in the Tablemodel, to go to the table it is added to, and change the background color of the Cell at Position (X,Y).

I have access to the JTable Object from my Tablemodel.


To Clarify the Problem, have a JTable that uses a Specific Tablemodel, and I need to highlight a cell when the value of that cell is changed.

The value change is done by calling a method that gives me the position on the datamodel.

setData(Object value, int row, int column)


Solution

  • I was able to do it, by using a small trick, i overode the

    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component comp = super.prepareRenderer(renderer, row, column);      
        if(((CTableModel)this.getModel()).getRowByNumber(row).isUpdated(column)){
            comp.setBackground(blinkColor);
            }
       return comp;
    }
    

    in my table model, i keep a list of row objects and in each object, i created a method to confirm if the column was updated.

    So i ask at the moment the renderer is running and done.