File dialog window in Java (filedialog, filenamefilter, jfilechooser, filefilter)

Source: Internet
Author: User

1. filedialog usage:

Filedialog FD = new filedialog (New Frame (), "test", filedialog. load );
Filenamefilter FF = new filenamefilter (){
Public Boolean accept (File Dir, string name ){
If (name. endswith ("jpg ")){
Return true;
}
Return false;
}
};
FD. setfilenamefilter (ff );
FD. setvisible (true );
System. Out. println (FD. getdirectory () + FD. GetFile ());

However, in windows, filedialog + filenamefilter cannot work normally. The original comment of jdoc is: Filename filters do not function in Sun's reference implementation for Microsoft Windows.

2. filedialog + filenamefilter can be replaced by jfilechooser + javax. Swing. filechooser. filefilter. The example in jdoc is as follows:

Jfilechooser chooser = new jfilechooser ();
// Note: source for examplefilefilter can be found in filechooserdemo,
// Under the demo/jfc directory in the Java 2 SDK, Standard Edition.
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 ());
}

To-application instance:
Jfilechooser filechooser = new jfilechooser (); // create a file Selector
Filechooser. setcurrentdirectory (new file ("."); // you can specify the current directory.
Filechooser. setacceptallfilefilterused (false );
// Display all objects
Filechooser. addchoosablefilefilter (New javax. Swing. filechooser. filefilter (){
Public Boolean accept (file F ){
Return true;
}
Public String getdescription (){
Return "all files (*.*)";
}
});
// Display the Java source file
Filechooser. setfilefilter (New javax. Swing. filechooser. filefilter (){
Public Boolean accept (file F) {// you can specify the suffix of an available file.
If (F. getname (). endswith (". Java") | f. isdirectory ()){
Return true;
}
Return false;
}
Public String getdescription (){
Return "Java source program (*. Java )";
}
});
// The setfilefilter method can be used repeatedly to set the jfilechooser selection type

 

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.