0

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;
    }
}
Gideon Sassoon
  • 544
  • 1
  • 9
  • 29

2 Answers2

1

If there are no mp3 files in the directory, obvl is still empty when the line

obvl.set(0, dir);

is executed.

Since there is no element with index 0 the exception is thrown. If you want to prepend dir to the list, use the add method:

obvl.add(0, dir);

BTW: The whole class has several issues:

  • The mainView field is never used.
  • You seem to create a second ObservableList for seemingly no reason.
  • listView.setItems(null); seems unnecessary
  • the ListView initially created may not be used, if a new one is set
  • multiple VBoxes containing a single ListView may be created, which may lead to the Node being used more than once
fabian
  • 80,457
  • 12
  • 86
  • 114
  • There should only be one Vbox and as far as I can see it's only used once. I believe there are multiple ObservableList because I was fiddling and trying to find a way to solve the problem. What I would like to show the list in the listView. I am not sure how to make that happen. – Gideon Sassoon Feb 08 '16 at 12:12
1

Trying to answer this question will be difficult as it's my first time, so I'll do the best I can. Also I realise it's technically should be FileListView I'll likely change this name later

What originally I was trying to do was add a list of io.File(s) to a list view. However creating in the constructor each time was incorrect as I only need one FileTreeView object.

When a user clicks an item in folder view (as seen in the first two columns of the screenshot) it is supposed to pass this folder path to FileTreeView which takes this information and loads all the files in the listView. Screenshot of Music Mohawk

The way to get around this is to initialise then pass FileTreeView to FolderTreeView

(The following is from a class called MainView)

this.fileTreeView = new FileTreeView(musicDataModel, musicDataView);
folderTreeView = new FolderTreeView(fileTreeView);
...
grid.addColumn(1, fileTreeView.listStack()); //Just so you know how listStack comes into play

On an event when a user clicks a folder FolderTreeView runs this line of code So instead of needing to incorrectly initialise a FileTreeView object it can use the existing fileTreeView which was passed on initialisation. This is from the FolderTreeView class.

fileTreeView.GetFilesFromFolder((newValue.getValue().toString()));

Finally here is the correct code from FileTreeView. Please note I am only posting code which relates to my problem. Any code related to the third column of the screenshot has not been included.

package musicmetadatak1009705;

import java.io.File;
import java.io.IOException;
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
 * http://stackoverflow.com/questions/35208522/javafx-listview-add-items
 * @author Scorchgid
 */
public class FileTreeView {

    public ObservableList<File> observableList;
    private ListView<File> listView;
    private VBox vbox;

    public ListView getListView() {
        return listView;
    }

    public void setListView(ListView listView) {
        this.listView = listView;
    }

    public FileTreeView() {
        this.observableList = FXCollections.observableArrayList();
        this.listView = new ListView(this.observableList);
    }

    public void GetFilesFromFolder(String dirName) throws IOException {
        File dir = new File(dirName);
        File[] files = dir.listFiles((File dir1, String filename) -> filename.endsWith(".mp3"));
        observableList.clear();
        observableList.addAll(files);
    }

    public void SetFileTreeView() throws IOException {
        listView.setItems(null);
    }

    public VBox listStack() throws IOException {
        vbox = new VBox();
        vbox.getChildren().add(listView);
        listView.setItems(observableList);
        return vbox;
    }
}
Gideon Sassoon
  • 544
  • 1
  • 9
  • 29