Java file Selection JFileChooser use example

Source: Internet
Author: User

the use of the JFileChooser class is very simple, mainly for the setting of some properties, and for the use of file filters.

[Java]

  1. import Javax.swing.JFileChooser;

  2. Public class Filechooser {

  3. Public Static void Main (string[] args)

  4. {

  5. JFileChooser FC = new jfilechooser ("D:");

  6. //Can be multi -selected

  7. Fc.setmultiselectionenabled (false);

  8. //Select mode to select files and folders

  9. Fc.setfileselectionmode (jfilechooser.files_and_directories);

  10. //Fc.setfileselectionmode (jfilechooser.files_only);  

  11. //Fc.setfileselectionmode (jfilechooser.directories_only);  

  12. //Set whether to show hidden files

  13. Fc.setfilehidingenabled (true);

  14. Fc.setacceptallfilefilterused (false);

  15. //Set file filter

  16. Fc.setfilefilter (new myfilter ("Java"));

  17. Fc.setfilefilter (new myfilter ("zip"));

  18. int returnvalue = Fc.showopendialog (null);

  19. if (returnvalue = = jfilechooser.approve_option)

  20. {

  21. //fc.getselectedfile ()

  22. //fc.getselectedfiles ()

  23. }

  24. }

  25. }

[Java]

  1. import java.io.File;

  2. import Javax.swing.filechooser.FileFilter;

  3. Public class Myfilter extends filefilter

  4. {

  5. Private String ext;

  6. Public Myfilter (String extstring)

  7. {

  8. this. ext = extstring;

  9. }

  10. Public Boolean accept (File f) {

  11. if (F.isdirectory ()) {

  12. return true;

  13. }

  14. String extension = getextension (f);

  15. if (Extension.tolowercase (). Equals (this. ext.tolowercase ()))

  16. {

  17. return true;

  18. }

  19. return false;

  20. }

  21. Public String GetDescription () {

  22. return this. ext.touppercase ();

  23. }

  24. Private String GetExtension (File f) {

  25. String name = F.getname ();

  26. int index = name.lastindexof ('. ');

  27. if (Index = =-1)

  28. {

  29. return "";

  30. }

  31. Else

  32. {

  33. return name.substring (index + 1). toLowerCase ();

  34. }

  35. }

  36. }

Java file Selection JFileChooser use example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.