java - 如何停止最小化、滚动 Pane 、调整大小对我所有 jtable 的着色

标签 java swing jtable background-color tablecellrenderer

正如你在我的照片中看到的:

最小化之前:

http://i.stack.imgur.com/yBbbt.png

最小化后

http://i.stack.imgur.com/UJXQ1.png

我的渲染器采用最后使用的颜色并绘制我的所有表格。

贝娄是我的自定义渲染器类:

public class MyCellRenderer extends DefaultTableCellRenderer {
public static double fstValue;
public static double sndValue;

public MyCellRenderer() { }
 
public MyCellRenderer(double fstValue, double sndValue) {
     this.fstValue = fstValue;
     this.sndValue = sndValue;
      //System.out.println(this.fstValue+" 2ndvalue"+this.sndValue+" ston constructor");
}
  @Override
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(!isSelected) {
            if(compare(this.fstValue,this.sndValue)== 1){
                c.setBackground(Color.GREEN);
               
            }else if (compare(this.fstValue,this.sndValue)== -1) {
                c.setBackground(Color.red);
              

            }else{
                c.setBackground(null);
            }
    }

            
        return c;
}
   
}

我正在快速更新表格,对此我没有任何问题。

But when I resize or minimize or scroll down, the coloring change.

When I minimize and resize, my table change color all, but when I scroll down only the table that I scrolled change color.

我怀疑它与我的渲染器调用的重绘或绘制方法有关,并且无法修复它。

我使用线程,每个线程都调用下面的代码进行更新:

if( home.text().equals(hometmp.toString())==false)
{
    MyCellRenderer cellRenderer = new MyCellRenderer(valuehm,valuehmt);                    
    table1.setValueAt(home.text(),i-1,1);
}

最佳答案

  1. 您对 super.getTableCellRendererComponent(...) 进行了两次调用。摆脱第二个。此外,无需将第一次调用强制转换为标签。该方法返回一个具有 setBackground() 方法的组件。

  2. 该方法不需要 synchronized 关键字。

关于java - 如何停止最小化、滚动 Pane 、调整大小对我所有 jtable 的着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36691039/

相关文章:

java - 将不同的类实例保存在列表中

java - 从 firestore 中检索所有文档作为自定义对象

java - 针对不同包中的相同类

java - 如何在 netbeans 可视化编辑器中的 JFrame 中添加弹出菜单

java - 画一个圆的半径和点周围的边缘

java - 当我将 xml 中的数据放入我的 customJTable 时,"prepareRenderer"不起作用

java - 如何防止Vertx自动写入日志?

java - 如何编写启动画面?

java - 更改 JTable 特定单元格中的字体颜色?

java - 是否有更有效的方法将 JTable 中已编辑的单元格内容重置回之前的内容