Adding multiple files with JFileChooser

十年热恋 提交于 2019-12-23 07:20:21

问题


I'm looking to make an application that allows you to attach files from your computer. For example, when the JFileChooser opens you have the option to select multiple files and when you click "add" it adds it to the window and you can then have different for what you want to do with those files. Similar to attaching files in an email. All the code I have to show is the JFileChooser because I figure I should learn how to do this before I go any further.

Thanks in advance

import javax.swing.JFileChooser;
import java.io.File;

public class Locket {

public static void main(String[] args) 
{
    JFileChooser chooser = new JFileChooser();
    File f = new File("Desktop");
    chooser.setCurrentDirectory(f);

    chooser.showOpenDialog(null);

}

}


回答1:


JFileChooser API already provides a method named getSelectedFiles(). You can use it for multiple selection. It returns an array of file objects, i.e. File[].




回答2:


You need to use JFileChooser#setMultiSelectionEnabled and set it to true. This will allow the user to be able to select multiple files



来源:https://stackoverflow.com/questions/13060371/adding-multiple-files-with-jfilechooser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!