JTable with cell flashing

我的梦境 提交于 2019-12-12 14:44:19

问题


I am writing an application using the Swing library in Java. I have a table component that extends JTable, and in this component I have overridden the method getTableCellRendererComponent, because I color the cells of the table. I have a custom table model (that extends from the default table model), and the table component itself I have added to a JPanel. All this works.

Now I would like to add to this table, some functionality to have a cell flashing. Potentially, more than one cell can be flashing at a time, i.e. cells at (row 1, column 2) and (row 3, column 4).

Is this possible to do? Any hints that could get me started would be appreciated.


回答1:


I find one article for your answer:

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

The page provide the source code for downloading.

Basically it use following method to notify table to update the cell timely.

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

After reading his code, I sort out a simpler version of his code, you can change my code or use his code (more elegant but also more complex).

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;
        }
    }
}


来源:https://stackoverflow.com/questions/10735758/jtable-with-cell-flashing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!