1

As you can see in my pictures:

Before minimize:

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

After minimize

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

My renderer takes the last color that have used and paints all my table.

Bellow is my custom renderer class:

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;
}
   
}

I m updating the table fast, and I have no problem with that.

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.

I suspect that it has something to do with the repaint or paint method that my renderer calls and have trouble fixing it.

I use threads and every thread calls the code below for the update:

if( home.text().equals(hometmp.toString())==false)
{
    MyCellRenderer cellRenderer = new MyCellRenderer(valuehm,valuehmt);                    
    table1.setValueAt(home.text(),i-1,1);
}
Community
  • 1
  • 1
Moglisss
  • 11
  • 3
  • `MyCellRenderer cellRenderer = new MyCellRenderer(valuehm,valuehmt); ` what are you doing with `cellRenderer` after creating it? – rdonuk Apr 18 '16 at 14:29
  • nothing i just want to pass the values to the class , so i can compare them to take the specific color – Moglisss Apr 18 '16 at 17:18

1 Answers1

1
  1. You have two calls to super.getTableCellRendererComponent(...). Get rid of the second. Also, there is no need to cast the first call to a label. The method return a Component which has a setBackground() method.

  2. You don't need the synchronized keyword on the method.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • i use threads and i was trying to synchronize but as you say it doent need it there, the super was a mistake i ll edit it now. The problem isnt there – Moglisss Apr 18 '16 at 17:20
  • Then the problem is in the code you didn't post. Post a proper [SSCCE](http://sscce.org/) that demonstrates the problem. So all you need is a frame with a JTable. The JTable will contain a few rows of Integer data. Then you custom renderer can highlight the Integers as required. The whole `SSCCE` will be about 30 lines of code. You will probably find the problem while creating the `SSCCE`. – camickr Apr 18 '16 at 18:54
  • I think the problem is in the static values , someone post it yesteraday but i cant find his ansewer maybe he delete it, because he wanted to put some code. I ll wait few days for his ansewer and then i ll post a proper SSCCE – Moglisss Apr 19 '16 at 09:12
  • Well static is never a good idea and should not be done So all you need to do is fix you code and test it. You don't need to wait for anybody to post code for something that simple. `I ll wait few days for his ansewer and then i ll post a proper SSCCE` - no, a SSCCE should be posted with every question when you state something doesn't work. You don't wait a couple of days. – camickr Apr 19 '16 at 14:25
  • i cant make simple my code so easy, i posted what i thought it was the problem because i have test all the other things. Now im trying to pass the values i want to my renderer without static so i m trying to figure that problem now. If you have an idea how to do that , i will be thankfull. – Moglisss Apr 19 '16 at 16:14
  • @Moglisss, `i cant make simple my code so easy` - yes you can. It takes one line of code to create a JTable: `JTable table = new JTable(5, 3);`. It takes two more lines of code to set a value in a couple of the rows: `table.setValueAt("1", 1, 1);`. Then you add your renderer to the table and test the renderer. You then need a couple more lines of code to create the frame and add the table to the frame. So you can create a simple test program to help you understand how a renderer works in about 10-15 lines of code, plus the renderer code. Just get rid of the static keyword. – camickr Apr 19 '16 at 16:27