0

Below I provide my replicable code. The problem is that it fills all cells with the gray color. However, I just need to make a gray-colored cell "moving" from column 0 to the last column (for the row 0).

Moreover, how to create more than one cell "moving" in a queue?

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.Timer;
import javax.swing.table.DefaultTableCellRenderer;

public class test2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI(); 
            }
        });     
    }

    private static void createAndShowGUI() {
        gtest t= new gtest("TEST");
        f.pack();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
    }
}  

class gtest extends JFrame
{

    private static JTable table;
    private int index;

    public gtest(String title)
    {
        table = new JTable(6, 10);
        table.setDefaultRenderer(Object.class, new PaintCell());
        add(table);
        startAnimation();       
    }

    private void startAnimation() {
        Timer timer = new Timer(100, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                index++;
                if (index > table.getRowCount() * table.getColumnCount())
                    index = 0;
                table.repaint();
            }
        });
        //timer.setRepeats(true);
        timer.start();
    }
    class PaintCell extends DefaultTableCellRenderer {
        private static final long serialVersionUID = 1L;
        public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus, int row,
                int column) {
            Component cell = super.getTableCellRendererComponent(table, value,
                    isSelected, hasFocus, row, column);
            int id = row * table.getRowCount() + column;
            cell.setBackground(id < index ? Color.LIGHT_GRAY : null);
            return cell;
        }
    }

}
Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217
  • [see why to avoids to call table.repaint();](http://stackoverflow.com/questions/16814512/tablecellrenderer-and-how-to-refresh-cell-background-without-using-jtable-repain), to store a Colors in model (some array) and by [changeing the Colors in TableCellRenderer](http://www.oracle.com/technetwork/java/christmastree-138396.html) – mKorbel Jan 30 '15 at 09:50
  • please whats "than one cell "moving" in a queue?", to cast "cell" to JLabel, as aside I'd suggest to covert index from view to model – mKorbel Jan 30 '15 at 09:53
  • @mKorbel: thanks. I am setting TableCellRenderer PaintCell, which extends DefaultTableCellRenderer. How can I dynamically change colors in PaintCell without table.repaint()? – Klausos Klausos Jan 30 '15 at 10:18

1 Answers1

1

Change...

int id = row * table.getRowCount() + column;
cell.setBackground(id < index ? Color.LIGHT_GRAY : null);

To...

int checkRow = index / table.getColumnCount();
int checkCol = index % table.getColumnCount();

cell.setBackground(checkRow == row && checkCol == column ? Color.LIGHT_GRAY : null);

This will calculate the row and column based on the current value of index and the compare to the cells row/column of the cell renderer

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366