簡體   English   中英

基於 Tiem 更新 JTable

[英]Update JTable Based on Tiem

我正在使用 JTable 來顯示由長時間運行的過程產生的值,並且我希望外觀隨着值“老化”而改變。 有一個時間戳指示每行數據最后一次更改的時間,當它超過一分鍾(或一小時)時,我想以不同的格式顯示它。

我可能會更改活動行的背景顏色,但我還沒有這樣做。

我的代碼的相關部分如下。 我打電話給 repaint,我試過打電話 fireTableDataChanged 沒有任何運氣。 舊值永遠不會更改為“非活動”格式。 如果我退出並重新啟動,舊值會以非活動格式繪制,但它不會動態發生。

private final long startup = System.currentTimeMillis ();

private static long minuteMillis = 1000 * 60;

private final SimpleDateFormat inactiveFormat = new SimpleDateFormat ("HH:mm");

private final SimpleDateFormat currentFormat = new SimpleDateFormat ("HH:mm:ss");

private long timestamp = 0; // Obtained from data

// Part of the table model
@Override // Subclass of DefaultTableModel
public Object getValueAt (int row, int column)
{
    if (timestamp < startup - minuteMillis)
    {
        return inactiveFormat.format (timestamp);
    }
    return currentFormat.format (timestamp);
}

final Thread thread = new Thread ()
{
    @Override
    public void run ()
    {
        while (true)
        {
            try
            {
                Thread.sleep (100);
                timestamp = readFromFile(); // Read from elsewhere
                // model.fireTableDataChanged (); // Does not help
                table.repaint (); // Does not update older values
            }
            catch (final Throwable e)
            {
                e.printStackTrace ();
            }
        }
    }
};
thread.start ();

我發現了問題。 上面代碼中的變量“startup”應該是 System.currentTimeMillis()。 這是一個工作演示,以防其他人想做類似的事情。 我認為這很有趣。

package demo;

import java.io.File;
import java.text.SimpleDateFormat;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import org.apache.logging.log4j.*;

public class TableDemo extends JPanel
{
    private final Logger logger = LogManager.getFormatterLogger (this.getClass ());

    public static void main (String[] args)
    {
        final TableDemo app = new TableDemo ();
        app.execute ();
        app.cycle ();
    }

    private final SimpleDateFormat inactiveFormat = new SimpleDateFormat ("HH:mm");

    private final SimpleDateFormat activeFormat = new SimpleDateFormat ("HH:mm:ss");

    /** Timestamp of learning data. */
    private final Long[][] timestamp = {{0l, 0l, 0l}, {0l, 0l, 0l}};

    private static long minuteMillis = 1000 * 10; // Reduced for testing

    /**
     * Create table model with data
     *
     * @see https://www.codejava.net/java-se/swing/a-simple-jtable-example-for-display
     */
    private final DefaultTableModel model = new DefaultTableModel ()
    {
        @Override
        public int getColumnCount ()
        {
            return timestamp[0].length;
        }

        @Override
        public int getRowCount ()
        {
            return timestamp.length;
        }

        @Override
        public Object getValueAt (int row, int column)
        {
            String result = "-";
            if (row < timestamp.length)
            {
                if (column < timestamp[0].length)
                {
                    final long value = timestamp[row][column];
                    if (value < System.currentTimeMillis () - minuteMillis)
                    {
                        result = inactiveFormat.format (value);
                    }
                    else
                    {
                        result = activeFormat.format (value);
                    }
                }
            }
            return result;
        }
    };

    private final JTable table = new JTable (model);

    private final JFrame frame = new JFrame ("Demo");

    public TableDemo ()
    {
        frame.setContentPane (new JScrollPane (table));
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    }

    public void execute ()
    {
        frame.pack ();
        frame.setVisible (true);
    }

    public void cycle ()
    {
        final Thread thread = new Thread ()
        {
            @Override
            public void run ()
            {
                // Make it easy to cd to the right place
                logger.info (new File ("data.txt").getAbsolutePath ());
                while (true)
                {
                    try
                    {
                        Thread.sleep (1000);
                        for (int i = 0; i < table.getRowCount (); i++)
                        {
                            // From bash:
                            // > touch data10.txt ; touch data02.txt
                            for (int j = 0; j < table.getColumnCount (); j++)
                            {
                                final String filename = String.format ("data%d%d.txt", i, j);
                                final File file = new File (filename);
                                if (file.exists ())
                                {
                                    timestamp[i][j] = file.lastModified ();
                                }
                                else
                                {
                                    timestamp[i][j] = 0L;
                                }
                            }
                        }
                        table.repaint ();
                    }
                    catch (final Throwable e)
                    {
                        e.printStackTrace ();
                    }
                }
            }
        };
        thread.start ();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM