如何使用java文件选择器在列表中添加项目

时间:2014-04-22 16:31:59

标签: java list jfilechooser

我正在使用java文件选择器从我的本地磁盘中选择一个文件并输出其名称和路径。有什么办法可以选择一个文件并将其添加到列表中吗?这是我的文件选择器的代码。

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());

1 个答案:

答案 0 :(得分:1)

我会使用JList。阅读How to Use Lists上Swing教程中的部分,了解更多信息和示例。

这个例子几乎就是你想要的。它向您展示了如何将字符串动态添加到JList的ListModel