0

.txtファイルからJtableを作成しています。.txtファイルは一定期間更新され続けます。実行時にJtableの.txtファイルにこれらの変更を反映する方法があるかどうかを知る必要があります!!! 再起動すると、テーブルは.txtファイルを読み取りますが、実行時にそれを行う方法はありますか?

4

5 に答える 5

3

テキストファイルの内容をチェックし続け、テーブルモデルを更新し続けるバックグラウンドスレッドを作成する必要があります。

于 2012-09-27T06:23:19.683 に答える
3

このようなものになると思います...

public class BackgroundMonitor implements Runnable {

    public void run() {

        while (true) {
            // Check to see if the file has changed since the last update...
            // If it has you will want to store the metrics and return true...
            if (hasChanged()) {

                // Load the contents into what ever construct you need to use
                ... = loadContents();

                // Create a new model...
                TableModel model = ... // create a new table model

                // Apply the model to the table...
                applyModel(model);

            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                Logger.getLogger(ThreadUpdates.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }

    protected void applyModel(final TableModel model) {

        // Apply the model to the table, making sure you update within the
        // EDT
        try {
            EventQueue.invokeAndWait(new Runnable() {

                @Override
                public void run() {
                    table.setModel(model);
                }
            });
        } catch (InterruptedException | InvocationTargetException exp) {
            // Handle the update exception....
        }

    }

}

これに関する問題は、テーブルを毎回完全に更新することを強制していることです。これは、(データの量が増えるにつれて) 時間が遅くなり、現在の選択が無効になる可能性があります。

最後の行を特定できる場合は、変更された行のみを追加することをお勧めします...

その場合、「適用」メソッドは次のようになります

protected void applyModel(final List<?> rowsToBeAdded) {

    // Apply the model to the table, making sure you update within the
    // EDT
    try {
        EventQueue.invokeAndWait(new Runnable() {
            @Override
            public void run() {

                MyUpdatableModel model = (MyUpdatableModel) table.getModel();
                model.addNewRows(rowsToBeAdded);

                // You will need to call fireTableRowsInserted(int firstRow, int lastRow)
                // indiciate where the new rows have been added, but this is best
                // done in the model

            }
        });
    } catch (InterruptedException interruptedException) {
    } catch (InvocationTargetException invocationTargetException) {
    }

}

これは、テーブルが更新された行を更新することのみを必要とし、選択に影響を与えないため、はるかに優れたアプローチです...

于 2012-09-27T06:39:45.313 に答える
2

書いたことに加えDanて、実際にバックグラウンド スレッド内でファイルを監視するには、WatchService APIを見ることができます。これはJava 7の SDK に追加されました。ファイルが変更されたときに通知されるイベントリスナーを登録できます。

于 2012-09-27T06:29:00.693 に答える
0

TableModelこれは、のメソッドを呼び出すことで実現できますfireTableDataChanged。私はあなたのテーブルがの実装によって支えられていると仮定していますAbstractTableModel. fireTableDataChangedその場合は、テキスト ファイルの内容を読み取り、テーブル モデルの値を更新した後に呼び出す必要があります。ここでいくつかの詳細を見つけることができます:

Java API

編集:以下のダンの返信に続いて-テキストファイル監視スレッドでfireTableDataChangedメソッドをトリガーする必要があります。

于 2012-09-27T06:27:15.267 に答える
0

自動更新もできません。私がしたことは、ウィンドウを閉じて、同じウィンドウをもう一度呼び出すことです。

于 2013-10-24T08:54:02.593 に答える