JTable with blinking cell

I am writing an application using the Swing library in Java. I have a table component that extends JTable, and in this component I override the method getTableCellRendererComponentbecause I color the table cells. I have a custom table model (which extends from the default table model), and I added the table component itself to JPanel. It all works.

Now I would like to add some functions to this table to blink a cell. Potentially, several cells may flash simultaneously, i.e. Cells in (row 1, column 2) and (row 3, column 4).

Can this be done? Any tips that might start me would be appreciated.

+3
source share
1 answer

:

http://www.devx.com/DevX/10MinuteSolution/17167/0/page/1

.

.

JTable.tableChanged(new TableModelEvent(table.getModel(), firstRow, lastRow, column));

, ( , ).

public class FlashCellTable
{
    public static Color color;

    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setSize(800, 600);

        final JTable table = new JTable(4, 4);
        table.setDefaultRenderer(Object.class, new MyFlashingCellRenderer());
        table.setValueAt("Flashing", 0, 0);
        frame.getContentPane().add(new JScrollPane(table));

        final long startTime = System.currentTimeMillis();

        Thread thread = new Thread()
        {
            @Override
            public void run()
            {
                while (true)
                {
                    long now = System.currentTimeMillis();
                    long second = (now - startTime) / 1000;
                    color = second / 2 * 2 == second ? Color.red : Color.blue;

                    System.out.println("FlashCellTable.run");

                    SwingUtilities.invokeLater(new Runnable()
                    {
                        public void run()
                        {
                            table.tableChanged(new TableModelEvent(table.getModel(), 0, 0, 0));  
                        }
                    });
                    try
                    {
                        Thread.sleep(1000);
                    }
                    catch(InterruptedException e)
                    {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                }
            }
        };

        thread.start();

        frame.setVisible(true);
    }

    public static class MyFlashingCellRenderer extends DefaultTableCellRenderer
    {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
                                                       int row, int column)
        {
            JLabel label =
                (JLabel)super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            if ("Flashing".equals(value))
            {
                label.setBackground(color);
            }
            else
            {
                label.setBackground(Color.white);
            }
            return label;
        }
    }
}
+3

All Articles