the use of the JFileChooser class is very simple, mainly for the setting of some properties, and for the use of file filters.
[Java]
-
import Javax.swing.JFileChooser;
-
-
Public class Filechooser {
-
Public Static void Main (string[] args)
-
{
-
JFileChooser FC = new jfilechooser ("D:");
-
//Can be multi -selected
-
Fc.setmultiselectionenabled (false);
-
//Select mode to select files and folders
-
Fc.setfileselectionmode (jfilechooser.files_and_directories);
-
//Fc.setfileselectionmode (jfilechooser.files_only);
-
//Fc.setfileselectionmode (jfilechooser.directories_only);
-
//Set whether to show hidden files
-
Fc.setfilehidingenabled (true);
-
Fc.setacceptallfilefilterused (false);
-
//Set file filter
-
Fc.setfilefilter (new myfilter ("Java"));
-
Fc.setfilefilter (new myfilter ("zip"));
-
-
int returnvalue = Fc.showopendialog (null);
-
if (returnvalue = = jfilechooser.approve_option)
-
{
-
//fc.getselectedfile ()
-
//fc.getselectedfiles ()
-
}
-
}
-
}
[Java]
-
import java.io.File;
-
import Javax.swing.filechooser.FileFilter;
-
-
Public class Myfilter extends filefilter
-
{
-
-
Private String ext;
-
-
Public Myfilter (String extstring)
-
{
-
this. ext = extstring;
-
}
-
Public Boolean accept (File f) {
-
if (F.isdirectory ()) {
-
return true;
-
}
-
-
String extension = getextension (f);
-
if (Extension.tolowercase (). Equals (this. ext.tolowercase ()))
-
{
-
return true;
-
}
-
return false;
-
}
-
-
-
Public String GetDescription () {
-
return this. ext.touppercase ();
-
}
-
-
Private String GetExtension (File f) {
-
String name = F.getname ();
-
int index = name.lastindexof ('. ');
-
-
if (Index = =-1)
-
{
-
return "";
-
}
-
Else
-
{
-
return name.substring (index + 1). toLowerCase ();
-
}
-
}
-
}
Java file Selection JFileChooser use example