如何从文本文件自动刷新JTable?

时间:2017-10-23 18:07:47

标签: java swing jtable jbutton

刷新数据的代码如下,但按下按钮后 每次我对文本文件进行更改时,我都需要运行代码。

这是我的代码:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    // Refresh Button
    try {
        for (int r = 0; r < 100; r++) { //initializing row
            for (int c = 0; c < 4; c++) { //initializing column
                jTable1.setValueAt(null, r, c);
            }
        }

        BufferedReader rdfile = new BufferedReader(new FileReader("items.txt"));

        String[] item = new String[100];
        String[] temp;

        int x = 0;  //read item
        while ((item[x] = rdfile.readLine()) != null) {
            temp = item[x].split("\t");
            jTable1.setValueAt((1000 + x + 1), x, 0);
            for (int j = 1; j < 4; j++) {
                jTable1.setValueAt(temp[j - 1], x, j);
            }

            x++;
        }
        rdfile.close();
    } catch (IOException e) {
    }
}

0 个答案:

没有答案