更新带有文件的JList

huangapple go评论55阅读模式
英文:

Updating a JList with Files

问题

我正在为学校项目尝试制作一个小程序用于提取文件我正在使用一个 JList 来显示所有提取的文件但在经过很多小时的探索后我无法弄清楚如何在文件夹中出现新文件时使这个 JList 更新由于批处理文件实际提取程序需要不同的时间来完成它在按钮的 ActionListener 中不能正确刷新我该如何让它工作

这是我用于查找特定扩展名文件的类
```java
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();
}

我在编码方面没有经验或者水平不高,所以任何帮助都将不胜感激!


<details>
<summary>英文:</summary>

I&#39;m trying to make a little program for a school project that extracts files. I&#39;m using a JList to display all the extracted files but after lots of hours lurking I can&#39;t figure out how to make this JList update when there are new files present in the folder. It doesn&#39;t refresh properly in the button&#39;s ActionListener since the batch file (the actual extractor) takes a varied amount of time to finish. How can I get this to work?

This is the class I&#39;m using to find files of certain extension:

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(&quot;.at9&quot;)) {
				return true;
			} else {
				return false;
			}
		}
	});
	String[] fileNames = new String[files.length];
	for (int i = 0; i &lt; files.length; i++) {
		fileNames[i] = files[i].getName();
	}

	return fileNames;
}

}

This is the JList:
	DefaultListModel model = new DefaultListModel();
	String[] things = fileFinder.thing();
	for (int i = 0; i &lt; things.length; i++) {
		model.addElement(things[i]);
	}
	JList list = new JList(model);
	list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	scrollPane.setViewportView(list);
And this is a button&#39;s ActionListener that starts a batch file which is the actual converter:

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();
}


I&#39;m not experienced or good in coding so any help would be appreciated!

</details>


# 答案1
**得分**: 0

&gt; Andrew Thompson: 另请参阅[当Runtime.exec()无法正常工作时](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html),了解有关正确创建和处理进程的许多有用提示。然后忽略它提到的 `exec`,并使用 `ProcessBuilder` 来创建该进程。还要将 `String arg` 拆分为 `String[] args`,以考虑包含空格字符的路径等情况。

我使用 `ProcessBuilder` 和 `waitFor()` 使其工作正常。

<details>
<summary>英文:</summary>

&gt; Andrew Thompson: See also [When Runtime.exec() won&#39;t](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html) 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.

 I got it working with a `ProcessBuilder` and `waitFor()`.

</details>



huangapple
  • 本文由 发表于 2020年5月3日 17:49:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/61572498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定