0

I am using the java file chooser in order to select a file from my local disk and output its name and path. Is there any way that I could select a file and add it to a list? Here is my code for the file chooser.

JFileChooser chooser = new JFileChooser();
File F = new File("C:/");
File namedir;
File namepath;    
chooser.setCurrentDirectory(F);
chooser.showOpenDialog(null);
chooser.setDialogTitle("Choose file ");
chooser.setApproveButtonText("View details");
namedir = chooser.getCurrentDirectory();
namepath = chooser.getSelectedFile();

JTextArea textarea = new JTextArea();
textarea.setEditable(false);
frame.add(textarea);

textarea.append("FILE LOCATION AND NAME:"+namepath);
System.out.print("the name of the the directory is "+namedir.getName());
System.out.print("the name of the the path is "+namepath.getAbsolutePath());
3
  • Are you trying to add the file object or the contents of the file to a list? Apr 22, 2014 at 16:36
  • What I want is to create a playlist with songs that the user can select from the local disk. So basically I need to allow the user to put in a playlist mp3 files Apr 22, 2014 at 16:37
  • I guess I don't see you can't use List<File> playlistFiles = new ArrayList<>(); playListFiles.add(namepath); Apr 22, 2014 at 16:39

1 Answer 1

1

I would use a JList. Read the section from the Swing tutorial on How to Use Lists for more information and examples.

The example is almost exactly what you want. It shows you how to dynamically add a String to the ListModel of the JList.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.