Laugh7 February 2016

JavaFX - Load Song object fields from text file into an ArrayList of Songs

I am trying to populate my ArrayList of Song objects with Songs that I have in a text file. I am running into so many issues and I am very confused. Can someone help me out? This is my code for the populateList method

public ArrayList<design.Song> populateList() throws FileNotFoundException{

        ArrayList<Song> songList = new ArrayList<>();

        Scanner sc = new Scanner(new File("/application/songs.txt"));



        while(sc.hasNext()){
            Song song1 = new Song(sc.nextLine(), sc.nextLine(), sc.nextLine(), Integer.parseInt(sc.nextLine()));
            songList.add(song1);
        }

        return songList;
    }

And here is my controller:

package design;

import application.Main;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Scanner;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;

public class Controller implements Initializable {

    @FXML private ListView<Song> listViewofSongs;
    List<Song> songArray = Main.populateList();



    @Override
    public void initialize(URL url, ResourceBundle rb) {
            listViewofSongs.setItems(FXCollections.observableList(songArray));
     }




    public void addSong(Song song){
        songArray.add(song);
    }
}

Answers


Laugh7 February 2016

The problems I was running into had to do with the fact that I was trying to implement code directly inside the Controller class. I need to implement all code inside some sort of method for the JavaFX controller.

Post Status

Asked in February 2016
Viewed 2,977 times
Voted 8
Answered 1 times

Search




Leave an answer