0

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)

rread
  • 143
  • 1
  • 13
  • *"I need a way for when I call a specific method in the datamodel, to go to the table it is added to, and change the background color of the Cell at Position (X,Y)."* ... why? What is the meaning of the different color? See [What is the XY problem?](http://meta.stackexchange.com/q/66377) – Andrew Thompson Mar 02 '21 at 21:27
  • *"I need to highlight a cell when the value of that cell is changed."* You might have a table with two columns for the old / new values. Use the renderer to determine if the 2nd column holds a (changed) value and change the BG color accordingly. – Andrew Thompson Mar 02 '21 at 21:36
  • *Custom Datamodel* - what is a Datamodel? I know a JTable uses a `TableModel`. A TableModel contains the data that is displayed in the table. If this is what you mean, then use the proper terminology. If this is not what you mean then the question is why do you have a separate model? The data should not be stored in two places – camickr Mar 02 '21 at 22:03
  • *I need to highlight a cell when the value of that cell is changed.* - check out: https://stackoverflow.com/questions/37003621/old-value-in-jtable-within-defualttablecellrenderer/37008800#37008800. It is an example of a renderer that allows individual cells to blink a different color. You should be able to remove the blinking logic to achieve a colored cell. – camickr Mar 02 '21 at 22:10
  • yes i mean Tablemodel its was a mistake in lingo – rread Mar 02 '21 at 22:53

1 Answers1

0

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.

rread
  • 143
  • 1
  • 13