• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to validate JFileChooser path ??

 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I am using JFileChooser to save and select files.
I want to validate the path I am entering in the path field before clicking Save .
Could anyone let me know how to do this ??


Thanks,
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a subclass that overrides approveSelection:
You can use getSelectedFile() and/or getSelectedFiles() to find out the current selection. I've used this technique to get a confirmation before selecting an existing file in a save dialog.
 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But how to call this method in my code and what criteria I should use?
In my case I want to check whether path entered for a file using JFileChooser is correct or not on the click of "Insert" button.
If the path is not valid then error should be displayed and code should not be excecuted further.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not related to the JFileChooser anymore. My example was for validating the file while you're selecting it. You want to do validation after you've selected it. After all, the JFileChooser is no longer visible when you press the Insert button.

You store the path of the selected file somewhere, don't you? A text field perhaps? Then simply get the path, create a new File again, then validate:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!