简体   繁体   English

JTable 从 DataModel 更改单元格背景颜色

[英]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.我有一个使用自定义表格模型的 JTable,在这个表格模型中我有不同的方法。

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).当我在 Tablemodel 中调用特定方法时,我需要一种方法,将 go 添加到它添加到的表中,并在 Position (X,Y) 处更改单元格的背景颜色。

I have access to the JTable Object from my Tablemodel.我可以从我的 Tablemodel 访问 JTable Object。


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.为了澄清问题,有一个使用特定表格模型的 JTable,当该单元格的值发生更改时,我需要突出显示该单元格。

The value change is done by calling a method that gives me the position on the datamodel.值更改是通过调用在数据模型上为我提供 position 的方法来完成的。

setData(Object value, int row, int column) setData(对象值,int 行,int 列)

I was able to do it, by using a small trick, i overode the我能够做到这一点,通过使用一个小技巧,我overode

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.在我的表 model 中,我保留了行对象列表,在每个 object 中,我创建了一种方法来确认列是否已更新。

So i ask at the moment the renderer is running and done.所以我现在问渲染器正在运行并完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM