0

Possible Duplicate:
How to select multiple files using java.awt.FileDialog

I'm writing a small java program that takes a file as input, reads the data, and prints the desire output to a file. I created a small JPanel where you can select the input and output files. I used FileDialog to select the input file. The program works great, but I want to know if I can have the ability to select few files in the FileDialog and it will return an array of files. because sometimes the input will be split between few files.

Please give me any ideas you have.

Thanks

Community
  • 1
  • 1
Mike
  • 1,302
  • 6
  • 23
  • 43
  • 2
    Use a [`JFileChooser`](http://docs.oracle.com/javase/6/docs/api/javax/swing/JFileChooser.html) instead. It allows you to select multiple files and return them as an array. – toniedzwiedz Nov 13 '12 at 17:18

4 Answers4

2

You can use setMultiSelectionEnabled(true); on your FileDialog, but only if you are using JDK7.

For older JDK versions the alternative is to use setMultiSelectionEnabled(boolean b) on a javax.swing.JFileChooser

dan
  • 13,132
  • 3
  • 38
  • 49
  • Thank you very much. that was an easy answer :) – Mike Nov 13 '12 at 17:37
  • I'm getting errors when doing **File[] selectedFiles = fd.getFiles();** – Mike Nov 13 '12 at 17:43
  • @Mike If your `fd` object is a `FileDialog`, it's because there is no such method: `getFiles()` in JDK5. Only starting with JDK7 that method exists. – dan Nov 13 '12 at 17:49
  • @Mike See the `Since: 1.7` from [FileDialog from JDK7 JavaDoc](http://docs.oracle.com/javase/7/docs/api/java/awt/FileDialog.html#getFiles()) – dan Nov 13 '12 at 17:51
0

Do you need to use AWT? If not then look at the swing JFileChooser. There is a setMultiSelectionEnabled method that will help

If you need the FileDialog component then look at this method

RNJ
  • 15,272
  • 18
  • 86
  • 131
0

Copied from How to select multiple files using java.awt.FileDialog

According to Javadoc it seems to be possible in JDK7 (see setMultipleMode(boolean) or getFiles() which returns an array of files). However cross checking with the Javadoc for JDK6 it is not possible in older versions...

Community
  • 1
  • 1
Robertiano
  • 352
  • 1
  • 6
0

Why not use a JFileChooser and call getSelectedFiles() ? Tutorial here.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440