File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JFileChooser: Selecting multiple files error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFileChooser: Selecting multiple files error" Watch "JFileChooser: Selecting multiple files error" New topic
Author

JFileChooser: Selecting multiple files error

Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

Hi,

Please have a look at the following code



In this code, I can select multiple files, but I am sure it is NOT selecting multiple files because "text.append("Opening "+file.getName()+"\n New Line\n");" code doesn't show names of multiple selected files, just a one file. Please help me..

Apart from this, I noticed I can't select files by simply CLICK AND DRAG system. How to enable that? Please help...


Are you better than me? Then please show me my mistakes..
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Why did you choose to call the getSelectedFile method, which obviously returns a File object, rather than the getSelectedFiles method, which obviously returns an array of Files?
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

Thanks a LOT for your quick reply. Actually I didn't know about that method because I was just learning about JFileChooser by my self through Oracle tutorials. Here is my new code.



This code works fine but there is a small issue. As you can see, this code accepts an array length of 5, which is fixed. But if someone need to select 30 files at ones, the array length will be insufficient. Is there a way to set the array length according to the number of files that user select? or else should I simply give an big array length like 100 ?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10425
    
    8

Check out the API to find out what JFileChooser#getSelectedFiles() actually returns.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

No. You don't decide how big the array is, the JFileChooser returns the array. It decides. You can certainly create your own array, as you did in that code, but that's pointless because the next line of code replaces it with the array returned from the JFileChooser.
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

wow! Thanks for both of your replies. Just gonna check that out, and will come back with the corrected code...

And sorry for the late reply.I didn't get any email notification..
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

OHHH YEAH!! Great, Perfect and excellent!!! This is what I call as learning!!! I did the code, have a look!




Thanks lot for all of you.. Since my primary objective is completed, I am marking this thread as solved, but I have another 2 questions.

I noticed I can't select files by simply CLICK AND DRAG system. How to enable that?

I don't like the way that the open dialog looks. It is just normal as we can see in other software. Got to know this is because of "Jave Look And Feel". Still I haven't learn about this, but if yes, I will definitely learn. Please help.,


Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

I found the answer for the look and feel.



Now I need an answer only for the CLICK AND DRAG thing. Please help..
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JFileChooser: Selecting multiple files error