Reputation: 1028
I'm creating an application that gets a list of .java and .class files from a chosen directory and places them in a JList. I am using Netbeans 7.1.2.
At the moment i have the files being retrieved and being placed into a List<File>
. I have all the files in the list printing out but i cannot add them to the JList the is in my form.
this is how i am adding the files to the list
List<File> filesInDirectory = new ArrayList<>();
public List listFilesForFolder(final File folder) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
if(fileEntry.toString().toUpperCase().endsWith(".CLASS") || fileEntry.toString().toUpperCase().endsWith(".JAVA")){
filesInDirectory.add(fileEntry);
System.out.println(fileEntry.getName());
}
}
}
return filesInDirectory;
}
Does anybody know how to do this?
Upvotes: 1
Views: 3042
Reputation: 1267
DefaultListModel model = new DefaultListModel();
for(File f : yourFileList) {
model.addElement(f);
}
yourList.setModel(model);
Upvotes: 5
Reputation: 285450
Create a DefaultListModel object and add the files to this, then have the JList use this model. For more on this, please have a look at the JList Tutorial. You will likely want to create a ListCellRenderer so your file text displays properly.
Upvotes: 4