• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 844
  • Last Modified:

JTable Cell Flickers with Renderer

I have created a TableCellRenderer that changes its fill depending on the cell value (below is the code). The problem I am getting is that when displayed it flickers like it was constantly being repainted.
Is there a way to instruct the renderer to only paint the one time? Or is this not actually the problem?
0
gdrnec
Asked:
gdrnec
  • 5
  • 2
1 Solution
 
gdrnecAuthor Commented:
Sorry, forgot to post the code:

    public class PercentCellRenderer implements TableCellRenderer {
       
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            Double perc = (Double)value;
            Color col = Color.red;
            if (perc.doubleValue() < .6) col = Color.yellow;
            if (perc.doubleValue() < .3) col = Color.green;
            Canvas c = new MeterCanvas(perc.doubleValue(), col);
            return c;
        }
       
        public class MeterCanvas extends Canvas {
           
            double percent;
            Color color;
           
            public MeterCanvas(double percent, Color color) {
                this.percent = percent;
                this.color = color;
            }
           
            public void paint(Graphics g) {
                g.setColor(color);
                g.fillRect(0,0, (int)(this.getWidth() * percent), this.getHeight());
            }
        }
       
    }
0
 
TimYatesCommented:
Can you post your renderer code?
0
 
TimYatesCommented:
  public class ColorTableCellRenderer extends JComponent implements TableCellRenderer {
        // The current color to display
        Color col ;
        Double perc ;
   
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
            perc = (Double)value;
            col = Color.red;
            if (perc.doubleValue() < .6) col = Color.yellow;
            if (perc.doubleValue() < .3) col = Color.green;
            return this;
        }
   
        // Paint current color
        public void paint(Graphics g) {
            g.setColor(curColor);
            g.fillRect(0, 0, (int)(this.getWidth() * percent) - 1, getHeight()-1);
        }
    }
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TimYatesCommented:
0
 
TimYatesCommented:
I think the problems with your code were:

1)  Canvas is an AWT class, so when you put it in a JTable, the draw order will be wrong
2)  You weren't overriding update in the Canvas class
3)  You were creating a new object each time the table rendered...

Hope my code work :-)

Tim
0
 
gdrnecAuthor Commented:
Yes. Tis perfect and the flickering is gone (whew! It was giving me seizures). Not sure why I complicated things with that Canvas. Thanks much.
0
 
TimYatesCommented:
There are 2 sets of classes you can use for doing GUIs (natively) in java...  

1)  AWT (the old way)
2)  Swing  (since java 1.2)

the problem with them is, that they cannot be combined into the same GUI, as they draw themselves in different order (so AWT will always seem to be "above" swing, and repaints go a bit mad...

http://java.sun.com/products/jfc/tsc/articles/mixing/

Glad I could help though :-)

Good luck with it!

Tim
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now