ListBox items load


Hi guys,

I'm making a playlist for a mp3 player and I want to load some songs from a directory.

My question is :

How do I load and display these mp3 songs to the listbox ?

I have already created the ListBox with the ListBoxModal class but I don't know how to load items in this listbox.

At least tell me what other classes do I need to make this possible.




Thanks for helping but what is shown in demo doesn't help me.

In the demo there are only 2 examples, one with Fonts and the other with Drag and Drop that demonstrates the codes of Listbox. But this is not my case because I have some mp3 files in a directory and I want to load them into the listbox but I'm not able to do so. I have searched all the functions of the classes and I didn't find a function that loads them. As I said I have no problem creating the ListBox, I just can't load the mp3 files. 



This will allow you to read in a list of files, and from there you can add them to your listbox.


I guess one way could be to iterate through a directory (or directories), testing if the file extension is mp3, and adding the file names to the list if they are. You could probably use a regex or File::getFileExtension() to test the extensions.




How do I load and display these mp3 songs to the listbox ?

Maybe you should specify more precisely, what you mean by loading and displaying...

Did you want to show length, artist info etc?  You can read metadata from the AudioFormatReader class:

I din't use this class yet, so I don't know how good the results meet your expectation, I've seen that the OGG format implements id3 tag attributes e.g. , you have to try if MP3 also reads usefull metadata...

Or do you want to show the waveform? Then you can use the AudioThumbnail class:

And for loading and playback see the JuceDemo unter Source/Demos/AudioPlaybackExample.cpp:

I hope that points to something useful, otherwise please rephrase your question...  ;-)


This is my code :

class Track : public Component, public ListBoxModel
            : iterator(File("C:\\Users\\Gerald\\Music"), true, "*.mp3")
            listBoxTra.setColour(ListBox::backgroundColourId, Colours::black.brighter(0.18f));
        void resized()
            listBoxTra.setBounds(0, 0, getParentWidth(), getParentHeight());
        int getNumRows()
            File folder("C:\\Users\\Gerald\\Music");
            return folder.getNumberOfChildFiles(File::findFiles, "*.mp3");
        void paintListBoxItem(int rowNumber, Graphics& a,
            int width, int height, bool rowIsSelected)
            if (!rowIsSelected) {
                a.setColour(Colour(0, 127, 255).brighter(0.3f));
                if (rowIsSelected) {

                    a.fillAll(Colour(0, 127, 255).brighter(0.08f));
            a.setFont(height * 0.695f);
            a.drawText(" The title of the song or mp3 file should be here but with the iterator class I can't get the name of the mp3 file. ",  
                5, 0, width, height,
                Justification::centredLeft, true);
        ListBox listBoxTra;
        DirectoryIterator iterator;

I can get the number of rows depending on the number of the mp3 files that I have in this folder 
("C:\\Users\\Gerald\\Music") , but I don't know how to add them in the ListBox. In this " C:\\..." directory there are 14 mp3 files that I want to add into the listbox and I want to add them all and display their name.

When the user clicks on one of the songs in the list it should start playing.

I guess DirectoryIterator doesn't help me because it gets only the file that the iterator is currently pointing at.

Maybe there is something else that I'm missing... ?


Thanks, but how can I add them to the listbox ? 

That's the problem... :/


I used DirectoryIterator Class but it didn't help me.

File::getFileExtension() only shows the extension of the file. What I want to do is that I need to show the names of all the mp3 files I have in that directory that I have set.


Thanks for the classes.

I have implemented a ListBox which is empty.

I want now to add mp3 tracks on each row of the list. I have a directory where the songs are. I just want to add them into the listbox.

After adding them into the listbox I need to play them. When the user clicks in one of the tracks listed it should start playing.

I hope you understood what I need to do.


Use a File class object. Pass it the directory you want to search when you create it. Then call its findChildFiles()* method to populate a file array with the files from that directory. You can then use the names of these files to populate your list box. 


Thanks, I understand how to pass the directory to the File class object, but how can I populate the file array then ?


Lets say I created this code :

Array<File> files;

File fileobj ("C:\\Users\\MusicDirectory\\");

fileobj.findChildFiles(files, File::findFiles, true, "*.mp3");


What do I do after this ? How can I populate the array ?



This actually works but I'm able only to get the first and the last element of the array...I have 14 mp3 files...How do I get the others ? ;/


How do I get the others ? ;/

By learning how to use arrays wink



That's what I thought.





I just learned the arrays. LOL

Finally found by myself how to add these mp3 files and get their name. laugh

I'm posting the code here if someone else needs it.

File folder("C:\\Users\\Gerald\\Music\\");
            folder.findChildFiles(files, File::findFiles, true, "*.mp3");
            a.drawText(" " + String(files.getReference(rowNumber).getFileName()),
                5, 0, width, height,
                Justification::centredLeft, true);

Thanks everyone who took the time to answer in this post. I appreciate it. yes


Yes, I just meant you can use that to test that each file is indeed a mp3 file. There's other member functions for getting the file name.


Thanks, File class was all I needed to do this but I was a little bit confused. ;)


thanks, it's probably useful info for a few of us too :D