Go to examplefilefilter. Java File

Source: Internet
Author: User

To restrict the file type when writing code for the open file dialog box today, the following methods are provided in the JDK help documentation:

Jfilechooser chooser = new jfilechooser ();
// Note: source for examplefilefilter can be found in filechooserdemo,
// Under the demo/jfc directory in the JDK.
Examplefilefilter filter = new examplefilefilter ();
Filter. addextension ("jpg ");
Filter. addextension ("GIF ");
Filter. setdescription ("JPG & GIF images ");
Chooser. setfilefilter (filter );
Int returnval = chooser. showopendialog (parent );
If (returnval = jfilechooser. approve_option ){
System. Out. println ("you chose to open this file:" +
Chooser. getselectedfile (). getname ());
}

The examplefilefilter class is used in the class and inherits the filefilter interface. It is stored in j2sdk1.4.2 _ 16 \ demo \ jfc \ filechooserdemo \ SRC. I use JDK 1.6, but this file is unavailable, after being depressed for a long time, I had to install j1.4 again. Now the problem is solved. I extracted the Code as follows:

Import java. Io. file;
Import java. util. hashtable;
Import java. util. enumeration;
Import javax. Swing .*;
Import javax. Swing. filechooser .*;
Public class examplefilefilter extends filefilter {

Private Static string type_unknown = "type unknown ";
Private Static string hidden_file = "hidden file ";

Private hashtable filters = NULL;
Private string description = NULL;
Private string fulldescription = NULL;
Private Boolean useextensionsindescription = true;
Public examplefilefilter (){
This. Filters = new hashtable ();
}
Public examplefilefilter (string extension ){
This (extension, null );
}

Public examplefilefilter (string extension, string description ){
This ();
If (extension! = NULL) addextension (Extension );
If (description! = NULL) setdescription (description );
}
Public examplefilefilter (string [] filters ){
This (filters, null );
}
Public examplefilefilter (string [] filters, string description ){
This ();
For (INT I = 0; I <filters. length; I ++ ){
// Add filters one by one
Addextension (filters [I]);
}
If (description! = NULL) setdescription (description );
}
Public Boolean accept (file F ){
If (F! = NULL ){
If (F. isdirectory ()){
Return true;
}
String extension = getextension (f );
If (extension! = NULL & filters. Get (getextension (F ))! = NULL ){
Return true;
};
}
Return false;
}
Public String getextension (file F ){
If (F! = NULL ){
String filename = f. getname ();
Int I = filename. lastindexof ('.');
If (I> 0 & I <FILENAME. Length ()-1 ){
Return filename. substring (I + 1). tolowercase ();
};
}
Return NULL;
}
Public void addextension (string extension ){
If (filters = NULL ){
Filters = new hashtable (5 );
}
Filters. Put (extension. tolowercase (), this );
Fulldescription = NULL;
}
Public String getdescription (){
If (fulldescription = NULL ){
If (description = NULL | isextensionlistindescription ()){
Fulldescription = Description = NULL? "(": Description + "(";
// Build the description from the extension list
Enumeration extensions = filters. Keys ();
If (extensions! = NULL ){
Fulldescription + = "." + (string) extensions. nextelement ();
While (extensions. hasmoreelements ()){
Fulldescription + = ",." + (string) extensions. nextelement ();
}
}
Fulldescription + = ")";
} Else {
Fulldescription = description;
}
}
Return fulldescription;
}
Public void setdescription (string description ){
This. Description = description;
Fulldescription = NULL;
}
Public void setextensionlistindescription (Boolean B ){
Useextensionsindescription = B;
Fulldescription = NULL;
}
Public Boolean isextensionlistindescription (){
Return useextensionsindescription;
}
}

 

Related Article

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.