1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
package BROWSE;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.Locale;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
* My perso JFileChooser
*
* @author t0163126
*/
public class FileChooser extends JFileChooser {
private static final long serialVersionUID = 1L;
private final FileFilter filterExcel;
private final String description;
private final String xls;
private final String xlsx;
private File curDir;
/**
* constructor
*/
public FileChooser() {
// Change the JVM local in english
Locale myLocale = Locale.ENGLISH;
Locale.setDefault(myLocale);
// Change the LookAndFeel :
UIManager.getDefaults().setDefaultLocale(myLocale);
// Change the new components
JComponent.setDefaultLocale(myLocale);
//The title dialog
UIManager.put("FileChooser.openDialogTitleText", "I'm here : ");
// change title
this.curDir = this.getCurrentDirectory();
this.setDialogTitle("" + this.curDir.getAbsolutePath());
this.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
File curDir = getCurrentDirectory();
setDialogTitle("" + curDir.getAbsolutePath());
}
}
});
// The look In label
UIManager.put("FileChooser.lookInLabelText", "Look In :");
// The file name label
UIManager.put("FileChooser.fileNameLabelText", "File name :");
// The file of type label
UIManager.put("FileChooser.filesOfTypeLabelText", "File type :");
// The open button
UIManager.put("FileChooser.openButtonText", "Load it");
//The open button tool tip
UIManager.put("FileChooser.openButtonToolTipText", "Load selected file");
// The cancel button
UIManager.put("FileChooser.cancelButtonText", "Cancel");
//The cancel button tool tip
UIManager.put("FileChooser.cancelButtonToolTipText","Cancel");
//The file name header button
UIManager.put("FileChooser.fileNameHeaderText","File name");
//The up folder button
UIManager.put("FileChooser.upFolderToolTipText", "Up one level");
//The desktop button tool tip
UIManager.put("FileChooser.homeFolderToolTipText","Desktop");
//The new folder button tool tip
UIManager.put("FileChooser.newFolderToolTipText","Create new folder");
//The list button tool tip
UIManager.put("FileChooser.listViewButtonToolTipText","List");
//The new folder button
UIManager.put("FileChooser.newFolderButtonText","Create new folder");
//The rename file button
UIManager.put("FileChooser.renameFileButtonText", "Rename file");
//The delete file button
UIManager.put("FileChooser.deleteFileButtonText", "Delete file");
//The details view button tool tip
UIManager.put("FileChooser.detailsViewButtonToolTipText", "Details");
//The size header
UIManager.put("FileChooser.fileSizeHeaderText","Size : ");
//The date header
UIManager.put("FileChooser.fileDateHeaderText", "Date modified : ");
/*
//Set icons for the JFileChooser
UIManager.put("FileView.directoryIcon", new ImageIcon(MyFileView.class.getResource("img/folder.png")));
UIManager.put("FileChooser.homeFolderIcon", new ImageIcon(MyFileView.class.getResource("img/user-home.png")));
UIManager.put("FileView.computerIcon", new ImageIcon(MyFileView.class.getResource("img/computer.png")));
UIManager.put("FIleView.floppyDriveIcon", new ImageIcon(MyFileView.class.getResource("img/media-floppy.png")));
UIManager.put("FileView.hardDriveIcon", new ImageIcon(MyFileView.class.getResource("img/drive-harddisk.png")));
UIManager.put("FileView.fileIcon", new ImageIcon(MyFileView.class.getResource("img/file.png")));
UIManager.put("FileChooser.upFolderIcon", new ImageIcon(MyFileView.class.getResource("img/go.png")));
UIManager.put("FileChooser.newFolderIcon", new ImageIcon(MyFileView.class.getResource("img/folder-new.png")));
UIManager.put("FileView.fileIcon", new ImageIcon(MyFileView.class.getResource("img/file.png")));
UIManager.put("FileChooser.listViewIcon", new ImageIcon(MyFileView.class.getResource("img/listIcon.png")));
UIManager.put("FileChooser.detailsViewIcon", new ImageIcon(MyFileView.class.getResource("img/details.png")));
*/
//Update UI
SwingUtilities.updateComponentTreeUI(this);
// Window explorer
this.setCurrentDirectory(new File("/"));
this.changeToParentDirectory();
//Add the filter to the JFileChooser
this.description = "Excel File (.xls, .xlsx)";
this.xls = "xls";
this.xlsx = "xlsx";
this.filterExcel = new FileNameExtensionFilter(this.description,
this.xls, this.xlsx);
this.addChoosableFileFilter(filterExcel);
}
/**
* choose a file
* @return the file
*/
public File fileChooser() {
JFileChooser fc = new FileChooser();
if ( fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION ) {
final File file = fc.getSelectedFile();
return file;//file.exists()&&fileFilter.accept(file)?file:null;
}
return null;
}
} |