Hi So I'm trying to add a list of files to my listView. It starts off by listStack being called to create a vBox (The view/stage is created elsewhere, this class just provides certain features) In another class a file directory is created listing folders. When a folder is clicked from this list it sends the location that was clicked and uses the constructor to add the items in the folder that are mp3 to the list so that the user can click on them.
TL;DR I just want to pass in a String and get a ListView of the files in that folder.
Note there's a lot of commented out lines of different tactics I have tried.
Also when I click a folder which has no files I get Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
My struggle is to update the list. I am happy to answer any questions you may have and if necessary paste code from the other files. Thank you for your time.
/*
* 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;
}
}