How to make List with all files in folder using swing?

邮差的信 提交于 2019-12-10 18:56:08

问题


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.

File folder = new File("c:/");
File[] listOfFiles = folder.listFiles();


回答1:


This example shows how to enumerate the files in a directory and display them in a JToolBar and a JMenu. You can use an Action, such as RecentFile, to encapsulate behavior for use in your ListModel and ListSelectionListener.




回答2:


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

String options = { "apple.exe", "ball.exe" "cat.exe"};
JList optionList = new JList(options);

Hope this will help you.




回答3:


See JFileChooser (shameless copy of the JFileChooser help page):

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?

setMultiSelectionEnabled (true); is another hint.

Location: java/docs/api/javax/swing/JFileChooser.html



来源:https://stackoverflow.com/questions/10373611/how-to-make-list-with-all-files-in-folder-using-swing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!