From: http://hi.baidu.com/gglzf4/blog/item/a950a402ff7ae67b3812bba2.html#0
Jfilechooser chooser = new jfilechooser ();
Chooser. setfileselectionmode (jfilechooser. directories_only); // you can only select folders.
Jfilechooser filechooser = new jfilechooser ();
Filechooser. setfileselectionmode (jfilechooser. files_only); // you can only select files.
Int flag = filechooser. showopendialog (this );
If (flag = jfilechooser. approve_option ){
File file = filechooser. getselectedfile ();
Attachhorizontallist. addnewattaches (File );
Jscrollpane. setborder (borderfactory. createemptyborder (0, 10, 10, 10 ));
// Jscrollpane. setborder (borderfactory. createtitledborder ("Total" + (+ + count) + "attachment "));
Jscrollpane. setverticalscrollbarpolicy (jscrollpane. vertical_scrollbar_never );
Jscrollpane. setopaque (false );
Jscrollpane. getviewport (). setopaque (false );
This. Add (jscrollpane );
This. repaint ();
}
Jfilechooser chooser = new jfilechooser ();
Chooser. setfileselectionmode (jfilechooser. directories_only );
Int flag = chooser. showsavedialog (mainframe );
If (flag = jfilechooser. approve_option ){
String path = chooser. getSelectedFile (). getAbsolutePath () + attachmentName;
Try {
FileInputStream ins = new FileInputStream (
AttachmentFile );
FileOutputStream out = new FileOutputStream (path );
Byte B [] = new byte [512];
While (ins. read (B)> 0 ){
Out. write (B );
}
Out. close ();
Ins. close ();
} Catch (ioexception E2 ){
Joptionpane. showmessagedialog (null, "failed to save ");
E2.printstacktrace ();
}
}
Design of the open dialog box and save dialog box swing Java
1. Click the OPEN button to open a dialog box.
Button. addactionlistener (New actionlistener (){
@ Override
Public void actionreceivmed (actionevent arg0 ){
// Todo auto-generated method stub
// Generate a file Selector
Jchooser = new jfilechooser ();
// Set the default Open Directory. If not set, follow the default directory of window (my document)
Jchooser. setcurrentdirectory (new file ("E :/"));
// Set the file type to open. Only folders can be selected, but files cannot be selected.
Jchooser. setfileselectionmode (jfilechooser. directories_only); // you can only open folders.
// Open a dialog box
Int Index = jchooser. showdialog (null, "Open File ");
If (Index = jfilechooser. approve_option ){
// Display the absolute path of the obtained file in the text editing box
JT. settext (jchooser. getselectedfile (). getabsolutepath ());
Readpath = JT. gettext () + "\\";
}
}
});
2. Click Save to open a save dialog box.
Button2.addactionlistener (New actionlistener (){
@ Override
Public void actionreceivmed (actionevent arg0 ){
// Todo auto-generated method stub
Jchooser2 = new jfilechooser ();
Jchooser2.setcurrentdirectory (new file ("E:/"); // sets the default open path.
Jchooser2.setdialogtype (jfilechooser. save_dialog); // set the Save dialog box
// Add the two configured file filters to the file selector.
Txtfilefilter = new txtfilefilter ();
Xlsfilefilter = new xlsfilefilter ();
JChooser2.addChoosableFileFilter (txtFileFilter );
JChooser2.addChoosableFileFilter (xlsFileFilter );
Int index = jChooser2.showDialog (null, "save file ");
If (index = JFileChooser. APPROVE_OPTION ){
File f = jChooser2.getSelectedFile ();
String fileName = jChooser2.getName (f) + ". xls ";
WritePath = jChooser2.getCurrentDirectory (). getAbsolutePath () + fileName;
Try {
WriteFile ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
// After the program is executed, a dialog box is displayed.
Int option = JOptionPane. showConfirmDialog (null, "Bitch: the result has been generated", "result ",
JOptionPane. YES_NO_OPTION );
System. exit (0 );
}
}
});
// Rewrite the file filter and set several optional file types in the open type. Here, two types are set: txt and xls.
Class txtfilefilter extends filefilter {
@ Override
Public Boolean accept (file F ){
// Todo auto-generated method stub
String namestring = f. getname ();
Return namestring. tolowercase (). endswith (". txt ");
}
@ Override
Public String getdescription (){
// Todo auto-generated method stub
Return "*. txt (Text File )";
}
}
Class xlsfilefilter extends filefilter {
@ Override
Public Boolean accept (file F ){
// Todo auto-generated method stub
String namestring = f. getname ();
Return namestring. tolowercase (). endswith (". xls ");
}
@ Override
Public String getdescription (){
// Todo auto-generated method stub
Return "*. xls (Table file )";
}
}