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

Java 如何从文本文件自动刷新JTable?,java,swing,jtable,jbutton,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;

下面是我刷新数据的代码,但在按下按钮之后 每次更改文本文件时,我都需要运行代码

这是我的密码:

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) {
    }
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
//刷新按钮
试一试{
对于(int r=0;r<100;r++){//初始化行
对于(int c=0;c<4;c++){//初始化列
jTable1.setValueAt(null,r,c);
}
}
BufferedReader rdfile=新BufferedReader(新文件读取器(“items.txt”);
字符串[]项=新字符串[100];
字符串[]温度;
int x=0;//读取项
而((项[x]=rdfile.readLine())!=null){
temp=项目[x]。拆分(“\t”);
jTable1.setValueAt((1000+x+1),x,0);
对于(int j=1;j<4;j++){
jTable1.setValueAt(温度[j-1],x,j);
}
x++;
}
rdfile.close();
}捕获(IOE异常){
}
}

每次更改文本文件时,我都需要运行代码
-为什么要更改文本文件?为什么不在更新文本文件的同时更新表呢?Java!=JavascriptYou可以使用swing计时器和file类的lastModified()方法来完成。但是您应该澄清您试图实现的目标…
}catch(IOException e){}
不要忽略异常!他们准确地告诉我们出了什么问题。除非实现日志记录,否则至少调用
Throwable.printStackTrace()
每次更改文本文件时,我都需要运行代码
-为什么要更改文本文件?为什么不在更新文本文件的同时更新表呢?Java!=JavascriptYou可以使用swing计时器和file类的lastModified()方法来完成。但是您应该澄清您试图实现的目标…
}catch(IOException e){}
不要忽略异常!他们准确地告诉我们出了什么问题。除非实现日志记录,否则至少调用
Throwable.printStackTrace()