3

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

enter image description here

mKorbel
  • 109,525
  • 20
  • 134
  • 319
kinkajou
  • 3,664
  • 25
  • 75
  • 128

3 Answers3

5

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

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.

UVM
  • 9,776
  • 6
  • 41
  • 66
1

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

user unknown
  • 35,537
  • 11
  • 75
  • 121