嗨所以我试图添加一个文件列表到我的listView。 它通过调用listStack来创建一个vBox(视图/舞台创建在其他地方,这个类只提供某些功能) 在另一个类中创建一个文件目录列出文件夹。当从该列表中单击文件夹时,它将发送被单击的位置并使用构造函数将文件夹中的项目添加到列表中,以便用户可以单击它们。javafx Listview add items
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;
}
}
应该只有一个Vbox,并且据我所见,它只能使用一次。 我相信有多个ObservableList因为我在摆弄并试图找到解决问题的方法。我想在listView中显示列表。我不知道如何做到这一点。 –