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,107
  • 18
  • 130
  • 305
kinkajou
  • 3,524
  • 24
  • 71
  • 124

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
  • 200,320
  • 28
  • 229
  • 974
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,725
  • 5
  • 40
  • 64
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
  • 34,093
  • 11
  • 73
  • 117