How to create a list with all the files in a folder using swing?

I want to create a list with a file name from a folder and show all the files that are present in this folder with a specific extension. I want the list to be selected so that I can select and delete a file from the list or edit it. I know how to select all files from a folder, but I don’t know how to show it in the GUI .

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

enter image description here

+3
source share
3 answers

This example shows how to list files in a directory and display them in JToolBarand JMenu. You can use Action, for example RecentFile, to encapsulate behavior for use in ListModeland ListSelectionListener.

+5
source

. JList swing. , -

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

, .

+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 file FilenameFilter?

setMultiSelectionEnabled (true); this is another hint.

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

+1
source

All Articles