更新带有文件的JList

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

Updating a JList with Files

问题

  1. 我正在为学校项目尝试制作一个小程序用于提取文件我正在使用一个 JList 来显示所有提取的文件但在经过很多小时的探索后我无法弄清楚如何在文件夹中出现新文件时使这个 JList 更新由于批处理文件实际提取程序需要不同的时间来完成它在按钮的 ActionListener 中不能正确刷新我该如何让它工作
  2. 这是我用于查找特定扩展名文件的类
  3. ```java
  4. public class fileFinder {
  5. public static String[] thing() {
  6. File file = new File(".\\at9snfsbs");
  7. File[] files = file.listFiles(new FilenameFilter() {
  8. @Override
  9. public boolean accept(File dir, String name) {
  10. if (name.toLowerCase().endsWith(".at9")) {
  11. return true;
  12. } else {
  13. return false;
  14. }
  15. }
  16. });
  17. String[] fileNames = new String[files.length];
  18. for (int i = 0; i < files.length; i++) {
  19. fileNames[i] = files[i].getName();
  20. }
  21. return fileNames;
  22. }
  23. }

这是 JList:

  1. DefaultListModel model = new DefaultListModel();
  2. String[] things = fileFinder.thing();
  3. for (int i = 0; i < things.length; i++) {
  4. model.addElement(things[i]);
  5. }
  6. JList list = new JList(model);
  7. list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  8. scrollPane.setViewportView(list);

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

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

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

  1. <details>
  2. <summary>英文:</summary>
  3. 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?
  4. 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() {

  1. @Override
  2. public boolean accept(File dir, String name) {
  3. if (name.toLowerCase().endsWith(&quot;.at9&quot;)) {
  4. return true;
  5. } else {
  6. return false;
  7. }
  8. }
  9. });
  10. String[] fileNames = new String[files.length];
  11. for (int i = 0; i &lt; files.length; i++) {
  12. fileNames[i] = files[i].getName();
  13. }
  14. return fileNames;
  15. }

}

  1. This is the JList:
  1. DefaultListModel model = new DefaultListModel();
  2. String[] things = fileFinder.thing();
  3. for (int i = 0; i &lt; things.length; i++) {
  4. model.addElement(things[i]);
  5. }
  6. JList list = new JList(model);
  7. list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  8. scrollPane.setViewportView(list);
  1. 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();
}

  1. I&#39;m not experienced or good in coding so any help would be appreciated!
  2. </details>
  3. # 答案1
  4. **得分**: 0
  5. &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`,以考虑包含空格字符的路径等情况。
  6. 我使用 `ProcessBuilder` `waitFor()` 使其工作正常。
  7. <details>
  8. <summary>英文:</summary>
  9. &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.
  10. I got it working with a `ProcessBuilder` and `waitFor()`.
  11. </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:

确定