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.