kinkajou
kinkajou

Reputation: 3728

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

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

Upvotes: 3

Views: 5119

Answers (3)

trashgod
trashgod

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

user unknown
user unknown

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

UVM
UVM

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

Related Questions