【发布时间】:2016-02-04 18:14:00
【问题描述】:
嗨所以我正在尝试将文件列表添加到我的 listView 中。 它首先调用 listStack 来创建一个 vBox(视图/舞台在其他地方创建,这个类只提供某些功能) 在另一个类中,创建了一个文件目录,列出了文件夹。当从这个列表中点击一个文件夹时,它会发送被点击的位置,并使用构造函数将文件夹中的 mp3 项目添加到列表中,以便用户可以点击它们。
TL;DR 我只想传入一个字符串并获取该文件夹中文件的 ListView。
请注意,我尝试过很多不同策略的注释行。
此外,当我单击一个没有文件的文件夹时,我得到Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
我的斗争是更新列表。 我很乐意回答您可能有的任何问题,如有必要,请粘贴其他文件中的代码。感谢您的宝贵时间。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package musicmetadatak1009705;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
/**
* http://stackoverflow.com/questions/1844688/read-all-files-in-a-folder
*
* @author Scorchgid
*/
public class FileTreeView {
private MainView mainView;
private ListView<File> listView = new ListView();
private ObservableList<File> obvl = FXCollections.observableArrayList();
private VBox vbox;
public ListView getListView() {
return listView;
}
public void setListView(ListView listView) {
this.listView = listView;
}
public FileTreeView() {
}
public FileTreeView(String dirName) throws IOException {
listView.setItems(null);
File dir = new File(dirName);
File[] files = dir.listFiles((File dir1, String filename) -> filename.endsWith(".mp3"));
ObservableList<File> oListStavaka = FXCollections.observableArrayList(files);
for (File file : files) {
System.out.println(file.toString());
}
//List myList = Arrays.asList(files);
obvl.addAll(files);
obvl.set(0, dir);
listView.setItems(oListStavaka);
System.out.println("Obvl to string" + obvl.toString());
System.out.println("Refersh");
System.out.println(listView.getItems());
}
//Files.walk(Paths.get(dir)).forEach((Path filePath) -> {
//if (Files.isRegularFile(filePath)) {
//System.out.println(filePath);
public VBox listStack() {
vbox = new VBox();
vbox.getChildren().add(listView);
// Path path = null;
// File file = new File(path.toString());
// listView.setRoot;
return vbox;
}
}
【问题讨论】:
标签: arrays file listview javafx observablelist