[英]How to make List with all files in folder using swing?
I want to make a list with filename from a folder and show all the files that are present in that folder with a particular extension. 我想从文件夹中创建一个包含文件名的列表,并显示该文件夹中存在的具有特定扩展名的所有文件。 I want the list to be selectable so that I can select and delete the file from the list or edit it.
我希望列表可以选择,以便我可以从列表中选择和删除文件或编辑它。 I know how to select all files from a folder but don't know how to show it in GUI .
我知道如何从文件夹中选择所有文件,但不知道如何在GUI中显示它。
File folder = new File("c:/");
File[] listOfFiles = folder.listFiles();
This example shows how to enumerate the files in a directory and display them in a JToolBar
and a JMenu
. 此示例显示如何枚举目录中的文件并将其显示在
JToolBar
和JMenu
。 You can use an Action
, such as RecentFile
, to encapsulate behavior for use in your ListModel
and ListSelectionListener
. 您可以使用
Action
(例如RecentFile
)来封装在ListModel
和ListSelectionListener
使用的行为。
You get all the file name from folder with extension and construct a string array out of that.Then use a JList to populate in swing.For example something like below 你从扩展名的文件夹中获取所有文件名,并从中构造一个字符串数组。然后使用JList填充swing。例如下面的内容
String options = { "apple.exe", "ball.exe" "cat.exe"};
JList optionList = new JList(options);
Hope this will help you. 希望这会帮助你。
See JFileChooser (shameless copy of the JFileChooser help page): 请参阅JFileChooser(JFileChooser帮助页面的无耻副本):
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
See the FilenameFilter? 请参阅FilenameFilter?
setMultiSelectionEnabled (true); setMultiSelectionEnabled(true); is another hint.
是另一个提示。
Location: java/docs/api/javax/swing/JFileChooser.html 地点:java / docs / api / javax / swing / JFileChooser.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.