使用文件更新 JList

Updating a JList with Files

我正在尝试为提取文件的学校项目制作一个小程序。我正在使用 JList 来显示所有提取的文件,但潜伏了很多小时后,当文件夹中存在新文件时,我无法弄清楚如何进行此 JList 更新。它在按钮的 ActionListener 中没有正确刷新,因为批处理文件(实际提取器)需要不同的时间才能完成。我怎样才能让它工作?

这是我用来查找特定扩展名文件的 class:

public class fileFinder {
    public static String[] thing() {
        File file = new File(".\at9snfsbs");
        File[] files = file.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                if (name.toLowerCase().endsWith(".at9")) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        String[] fileNames = new String[files.length];
        for (int i = 0; i < files.length; i++) {
            fileNames[i] = files[i].getName();
        }

        return fileNames;
    }

}

这是 JList:

        DefaultListModel model = new DefaultListModel();
        String[] things = fileFinder.thing();
        for (int i = 0; i < things.length; i++) {
            model.addElement(things[i]);
        }
        JList list = new JList(model);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        scrollPane.setViewportView(list);

这是一个启动批处理文件的按钮的 ActionListener,它是实际的转换器:

try {
    Runtime.getRuntime().exec("cmd /c start .\at9snfsbs\shit.bat");
    String[] thang = fileFinder.thing();
    model.clear();
    for (int i = 0; i < thang.length; i++) {
        model.addElement(thang[i]);
    };
} catch (IOException e1) {
    e1.printStackTrace();
}

我没有经验或不擅长编码,所以任何帮助将不胜感激!

Andrew Thompson: See also When Runtime.exec() won't for many good tips on creating and handling a process correctly. Then ignore it refers to exec and use a ProcessBuilder to create the process. Also break a String arg into String[] args to account for things like paths containing space characters.

我用 ProcessBuilderwaitFor() 得到了它。