The file selector JFileChooser in Java (how to set the default filename and display in the box) summary __java

Source: Internet
Author: User
Tags save file

In Java GUI applications, it is unavoidable to use the File selection box JFileChooser, which can be used to select open files (Jfilechooser1.setfileselectionmode jfilechooser.open_dialog) and save the file (Jfilechooserout.setfileselectionmode (Jfilechooser.save_dialog)), although Setfileselectionmode () can set how the selector is used. But this method is sometimes best not to use, such as when you want to save the file, when you specify a default file, this method will not achieve the default effect.

Now, if you do the default filename, the effect in the following figure. Where the Track.java file name is automatically default.


1.String Defaultdisk = "e:/";

String defaultfilename = "Track.java";

JFileChooser jFileChooser1 = new JFileChooser ();//Instance Selector

Jfilechooser1.setcurrentdirectory (New File (Defaultdisk))//Set default directory open direct default e disk

Jfilechooser1.setfileselectionmode (Jfilechooser.save_dialog)//Save here Remove otherwise default filename cannot be used
Jfilechooser1.setdialogtitle ("Save File"); Custom Selection box title
Jfilechooser1.setselectedfile (New File (Defaultfilename)); Set Default filename
Jfilechooser1.showopendialog (my);
int ch = jfilechooser1.showsavedialog (my);//You can't use Showopendialog and showsavedialog here either.

int ch = Jfilechooser1.showdialog (My, "Save File");//This line of code replaces Showopendialog and Showsavedialog

If you also want to restrict file types

Private String savetype[] = {"TXT", "Java"}; //

Jfilechooser1.setfilefilter (New Filenameextensionfilter ("TXT & JAVA FILE", Savetype));

This is the key code, the other is not written, if you have a problem, leave a message.

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.