Reputation: 3728
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();
Upvotes: 3
Views: 5119
Reputation: 205785
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
.
Upvotes: 5
Reputation: 36229
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
Upvotes: 1
Reputation: 9914
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.
Upvotes: 1