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?
LVL 4
gdrnecAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.