Animation for table cell renderer

Code shown below is a complete failure. Nothing is displayed on the target cell let alone do animation. What's wrong with this code?

import javax.swing.*;

import javax.swing.table.*;

import java.awt.*;

import java.awt.event.*;

publicclass AnimeTableCell{

JFrame frame;

Container con;

JScrollPane scp;

JTable table;

AnimePanel anp, ant;

Image[] images;

public AnimeTableCell(){

frame =new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

con = frame.getContentPane();

images =new Image[4];

images[0] =new ImageIcon("images/a.png").getImage();

images[1] =new ImageIcon("images/b.png").getImage();

images[2] =new ImageIcon("images/c.png").getImage();

images[3] =new ImageIcon("images/d.png").getImage();

JTable table =new JTable(10, 6){

public Component preapareRenderer

(TableCellRenderer renderer,int row,int column){

Component c = super.prepareRenderer(renderer, row, column);

if (row == 3 && column == 2){

if (ant !=null){

c = ant;

}

}

return c;

}

};

scp =new JScrollPane(table);

con.add(scp, BorderLayout.CENTER);

ant =new AnimePanel(images, 500,true);//for cell renderer, repaint table

anp =new AnimePanel(images, 500,false);//this doesn't repaint table

con.add(anp, BorderLayout.SOUTH);

frame.setSize(400, 400);

frame.setVisible(true);

}

class AnimePanelextends JPanelimplements ActionListener{

Image[] imgs;

int delay;

boolean trepaint;

int range;

int idx;

Timer timer;

public AnimePanel(Image[] ima,int de,boolean r){

setPreferredSize

(new Dimension(ima[0].getWidth(this), ima[0].getHeight(this)));

imgs = ima;

delay = de;

trepaint = r;

range = ima.length - 1;

idx = 0;

timer =new Timer(delay,this);

timer.start();

}

publicvoid actionPerformed(ActionEvent e){

++idx;

if (idx > range){

idx = 0;

}

repaint();

if (table !=null && trepaint){

table.repaint();

}

}

publicvoid paintComponent(Graphics g){

super.paintComponent(g);

g.drawImage(imgs[idx], 0, 0,this);

}

}

publicstaticvoid main(String[] args){

new AnimeTableCell();

}

}

[5102 byte] By [hiwaa] at [2007-10-3 10:13:24]
# 1

2 main reasons:

a) "table" is defined as a class and local variable, so its always null and the repaint is never done

b) the "prepareRenderer" method is spelt incorrectly

However, that still doesn't fix the problem. the AnimePanel is not a real panel (since its not added to any container), so overriding the paintComponent() method doesn't do anything.

So, just extend JLabel, and use the setIcon method to change the icon before invoking the repaint.

Also, setting the preferredSize of the component won't do anything since the table will set the size of the renderer when it paints the cell.

Invoking repaint() on the entire table is not very efficient. Here is a posting that has blinking background colors of cells. Only the cells that blink are repainted. You may be able to incorporate some of the concepts in your solution:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=606504

camickra at 2007-7-15 5:33:44 > top of Java-index,Desktop,Core GUI APIs...
# 2

> "table" is defined as a class and local variable

> the "prepareRenderer" method is spelt incorrectly

Wow, most typical newbie errors, what a shame!

> However, that still doesn't fix the problem

Fixing the above two errors has brought the animation on the target cell. Miraculous!

> You may be able to incorporate some of the concepts in your solution:

> http://forum.java.sun.com/thread.jspa?forumID=57&threadID=606504

Mmm..., I didn't aware of using fireTableCellUpdated() method. Thanks for giving a useful idiom.

hiwaa at 2007-7-15 5:33:45 > top of Java-index,Desktop,Core GUI APIs...