下面列出了javax.swing.JFileChooser#setFileSelectionMode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void onUnCompressFile(Compressor ma) {
File file = getSelectedArchiverFile(ma.getFileFilter());
if (file == null) {
return;
}
String fn = file.getName();
fn = fn.substring(0, fn.lastIndexOf('.'));
JFileChooser s = new JFileChooser(".");
s.setSelectedFile(new File(fn));
s.setFileSelectionMode(JFileChooser.FILES_ONLY);
int returnVal = s.showSaveDialog(this);
if (returnVal != JFileChooser.APPROVE_OPTION) {
return;
}
String filepath = s.getSelectedFile().getAbsolutePath();
try {
ma.doUnCompress(file, filepath);
} catch (IOException e) {
e.printStackTrace();
}
}
private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
String command = evt.getActionCommand();
if ("BROWSE".equals(command)) {
JFileChooser chooser = new JFileChooser();
FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
chooser.setDialogTitle("Select Project Location");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
String path = this.projectLocationTextField.getText();
if (path.length() > 0) {
File f = new File(path);
if (f.exists()) {
chooser.setSelectedFile(f);
}
}
if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
File projectDir = chooser.getSelectedFile();
projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath());
}
panel.fireChangeEvent();
}
}
private void chooseFile(JTextField txtField) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(null);
chooser.setFileSelectionMode (JFileChooser.FILES_ONLY);
String path = txtField.getText().trim();
if (path != null && path.length() > 0) {
chooser.setSelectedFile(new File(path));
} else if (recentDirectory != null) {
chooser.setCurrentDirectory(new File(recentDirectory));
}
if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) {
return;
}
File selectedFile = chooser.getSelectedFile();
recentDirectory = selectedFile.getParentFile().getAbsolutePath();
txtField.setText(selectedFile.getAbsolutePath());
}
public static void main(String[] args) throws IOException {
SwingManagedJSettlers.setupResources(true, args);
ByteReader file = openSoundFile();
int[][] starts = getSoundStarts(file);
final JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File base = fc.getSelectedFile();
for (int i = 0; i < starts.length; i++) {
File groupDir = new File(base, "" + i);
groupDir.mkdir();
for (int j = 0; j < starts[i].length; j++) {
File seqFile = new File(groupDir, j + ".wav");
short[] data = getSoundData(file, starts[i][j]);
exportTo(data, seqFile);
System.out.println("Exported file " + i + "." + j);
}
}
}
}
private JFileChooser getJFileChooser() {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(NbBundle.getMessage(AddServerLocationVisualPanel.class, "LBL_ChooserName")); //NOI18N
chooser.setDialogType(JFileChooser.CUSTOM_DIALOG);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setApproveButtonMnemonic("Choose_Button_Mnemonic".charAt(0)); //NOI18N
chooser.setMultiSelectionEnabled(false);
chooser.setApproveButtonToolTipText(NbBundle.getMessage(AddServerLocationVisualPanel.class, "LBL_ChooserName")); //NOI18N
chooser.getAccessibleContext().setAccessibleName(NbBundle.getMessage(AddServerLocationVisualPanel.class, "LBL_ChooserName")); //NOI18N
chooser.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(AddServerLocationVisualPanel.class, "LBL_ChooserName")); //NOI18N
// set the current directory
chooser.setSelectedFile(new File(locationTextField.getText().trim()));
return chooser;
}
private void onSelectFileButtonClick() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(selectionMode);
File currentFile = new File(filePathField.getText());
if (currentFile.exists()) {
if (currentFile.isDirectory()) {
fileChooser.setCurrentDirectory(currentFile);
} else {
fileChooser.setCurrentDirectory(currentFile.getParentFile());
}
}
int status = fileChooser.showOpenDialog(this);
if (status == JFileChooser.APPROVE_OPTION) {
// only one file is allowed
File chosenFile = fileChooser.getSelectedFile();
callBack.call(chosenFile);
filePathField.setText(chosenFile.getPath());
}
}
private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
String command = evt.getActionCommand();
if ("BROWSE".equals(command)) {
JFileChooser chooser = new JFileChooser();
FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
chooser.setDialogTitle("Select Project Location");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
String path = this.projectLocationTextField.getText();
if (path.length() > 0) {
File f = new File(path);
if (f.exists()) {
chooser.setSelectedFile(f);
}
}
if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
File projectDir = chooser.getSelectedFile();
projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath());
}
panel.fireChangeEvent();
}
}
/**
* Action performed.
*
* @param event the event
* @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent event) {
try {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Load AE specifier file");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (this.main.getAnnotOpenDir() != null) {
fileChooser.setCurrentDirectory(this.main.getAnnotOpenDir());
}
int rc = fileChooser.showOpenDialog(this.main);
if (rc == JFileChooser.APPROVE_OPTION) {
this.main.loadAEDescriptor(fileChooser.getSelectedFile());
}
this.main.setAllAnnotationViewerItemEnable(false);
} finally {
this.main.resetCursor();
}
}
public static File chooseFile(final Component parent, final boolean filesOnly, final String title, final File selectedFile, final FileFilter filter) {
final JFileChooser chooser = new JFileChooser(selectedFile);
chooser.setApproveButtonText("Select");
if (filter != null) {
chooser.setFileFilter(filter);
}
chooser.setDialogTitle(title);
chooser.setMultiSelectionEnabled(false);
if (filesOnly) {
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
} else {
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
}
if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
return chooser.getSelectedFile();
} else {
return null;
}
}
private void onUnArchiverFile(Archiver ma) {
File f = getSelectedArchiverFile(ma.getFileFilter());
if (f == null) {
return;
}
JFileChooser s = new JFileChooser(".");
s.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = s.showSaveDialog(this);
if (returnVal != JFileChooser.APPROVE_OPTION) {
return;
}
String filepath = s.getSelectedFile().getAbsolutePath();
String password = null;
while (true) {
try {
ma.doUnArchiver(f, filepath, password);
break;
} catch (WrongPassException re) {
password = JOptionPane.showInputDialog(this,
"压缩文件疑似已加密,请输入解压密码");
if (password == null) {
return;
}
} catch (IOException ioe) {
ioe.printStackTrace();
break;
}
}
}
private void browseLicenceButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseLicenceButtonActionPerformed
JFileChooser chooser = new JFileChooser(ModuleUISettings.getDefault().getLastChosenLibraryLocation());
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setMultiSelectionEnabled(false);
if (txtLicense.getText().trim().length() > 0) {
chooser.setSelectedFile(new File(txtLicense.getText().trim()));
}
int ret = chooser.showDialog(this, getMessage("LBL_Select"));
if (ret == JFileChooser.APPROVE_OPTION) {
txtLicense.setText(chooser.getSelectedFile().getAbsolutePath());
ModuleUISettings.getDefault().setLastChosenLibraryLocation(txtLicense.getText());
}
}
public EvolutionPanel(JFrame _f, Component _p)
{
//--- set-up auxillary panels-----
this.frame = _f;
parent = _p;
i1 = 0;
//sthis.setPreferredSize(new Dimension(1000,700));
curDir=System.getProperty("user.dir") + File.separator;
fc = new JFileChooser(curDir);
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
df = new DecimalFormat("##.##");
df2 = new DecimalFormat("##.###");
df3 = new DecimalFormat("##.####");
df4 = new DecimalFormat("##.#####");
filter_stock_choice = new String("all");
M = 5; nlag = 10; n_saved_perf = 0;
setupDataPanel();
setupStrategyPanel();
setupMetaFilterPanel();
metaToFile = new ArrayList<METAFilterParameters>();
black_list = new int[0][0];
portfolio_invest = new ArrayList<JInvestment[]>();
strategies = new ArrayList<StrategyParameters>();
//setup Evolution Components
initComponents();
}
private void ButtonSelFileActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_ButtonSelFileActionPerformed
{//GEN-HEADEREND:event_ButtonSelFileActionPerformed
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
SelFile = fc.getSelectedFile();
FilePathTextField.setText(SelFile.getAbsolutePath());
NameTextField.setText(SelFile.getName());
}
}
public static File browseFile(String path) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
File dir = new File(path);
if (dir.exists())
fc.setCurrentDirectory(dir);
int ret = fc.showOpenDialog(null);
if (ret == JFileChooser.APPROVE_OPTION) {
File file = new File(fc.getSelectedFile().getAbsolutePath());
if (file.exists())
return file;
}
return null;
}
private static JFileChooser createChooser() {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
return chooser;
}
/**
* Display a save dialog
*
* @param Parent Parent windows
* @param Directory Directory to select when the dialog is displayed
* @param DefaultFileName Default filename. Empty string if none
* @param Extension File extention (ie: ".myp")
* @param FilterText Text filter (ie: "File GPX (*.gpx)|*.gpx")
* @param TestFileExist Test the file exist
* @param FileExistText Text displayed if the selected file exist (Over write
* confirmation)
* @return Filename with path. Empty if cancel
*/
public static String SaveDialog(Component Parent, String Directory, String DefaultFileName, String Extension,
String FilterText, Boolean TestFileExist, String FileExistText) {
JFileChooser fileChooser = new JFileChooser();
if (DefaultFileName.isEmpty())
fileChooser.setCurrentDirectory(new File(Directory));
else
fileChooser.setSelectedFile(new File(DefaultFileName));
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
FileFilter mypFilter = new FileTypeFilter(Extension, FilterText);
fileChooser.addChoosableFileFilter(mypFilter);
fileChooser.setFileFilter(mypFilter);
int result = fileChooser.showSaveDialog(Parent);
if (result == JFileChooser.APPROVE_OPTION) {
String selectedFile = fileChooser.getSelectedFile().getAbsolutePath();
if (Utils.GetFileExtension(selectedFile).isEmpty()) {
selectedFile = selectedFile + Extension;
}
boolean ok = true;
if (Utils.FileExist(selectedFile) && (TestFileExist)) {
if (JOptionPane.showConfirmDialog(Parent, FileExistText, "",
JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION)
ok = false;
}
if (ok)
return selectedFile;
else
return "";
} else
return "";
}
private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
final JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode (JFileChooser.FILES_ONLY);
chooser.setMultiSelectionEnabled(false);
chooser.setFileFilter(new SplashFileFilter());
if (lastImageFolder != null) {
chooser.setCurrentDirectory(lastImageFolder);
}
chooser.setDialogTitle(NbBundle.getMessage(CustomizerApplication.class, "LBL_Select_Splash_Image"));
if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
File file = FileUtil.normalizeFile(chooser.getSelectedFile());
splashTextField.setText(file.getAbsolutePath());
lastImageFolder = file.getParentFile();
}
}
private String selectExportDir() {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(Configuration.lastExportDir.get()));
chooser.setDialogTitle(translate("export.select.directory"));
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
final String selFile = Helper.fixDialogFile(chooser.getSelectedFile()).getAbsolutePath();
Configuration.lastExportDir.set(Helper.fixDialogFile(chooser.getSelectedFile()).getAbsolutePath());
return selFile;
}
return null;
}
public static Path chooseGameDataFolder() throws FileNotFoundException{
JFileChooser chooser = new JFileChooser(lastZipLocation != null ? lastGameDataLocation.toFile() : null);
chooser.setDialogTitle("Please select the GameData folder");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); // Only accept folders
return lastGameDataLocation = showOpenDialog(chooser);
}
public void exportSnapshots(final FileObject[] selectedSnapshots) {
assert (selectedSnapshots != null);
assert (selectedSnapshots.length > 0);
final String[] fileName = new String[1], fileExt = new String[1];
final FileObject[] dir = new FileObject[1];
if (selectedSnapshots.length == 1) {
SelectedFile sf = selectSnapshotTargetFile(selectedSnapshots[0].getName(),
selectedSnapshots[0].getExt().equals(HEAPDUMP_EXTENSION));
if ((sf != null) && checkFileExists(sf)) {
fileName[0] = sf.fileName;
fileExt[0] = sf.fileExt;
dir[0] = sf.folder;
} else { // dialog cancelled by the user
return;
}
} else {
JFileChooser chooser = new JFileChooser();
if (exportDir != null) {
chooser.setCurrentDirectory(exportDir);
}
chooser.setDialogTitle(Bundle.ResultsManager_SelectDirDialogCaption());
chooser.setApproveButtonText(Bundle.ResultsManager_SaveButtonName());
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setMultiSelectionEnabled(false);
if (chooser.showSaveDialog(WindowManager.getDefault().getMainWindow()) == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
if (!file.exists()) {
if (!ProfilerDialogs.displayConfirmation(
Bundle.ResultsManager_DirectoryDoesntExistMsg(),
Bundle.ResultsManager_DirectoryDoesntExistCaption())) {
return; // cancelled by the user
}
file.mkdir();
}
exportDir = file;
dir[0] = FileUtil.toFileObject(FileUtil.normalizeFile(file));
} else { // dialog cancelled
return;
}
}
final ProgressHandle ph = ProgressHandle.createHandle(Bundle.MSG_SavingSnapshots());
ph.setInitialDelay(500);
ph.start();
ProfilerUtils.runInProfilerRequestProcessor(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < selectedSnapshots.length; i++) {
exportSnapshot(selectedSnapshots[i], dir[0], fileName[0] != null ? fileName[0] : selectedSnapshots[i].getName(), fileExt[0] != null ? fileExt[0] : selectedSnapshots[i].getExt());
}
} finally {
ph.finish();
}
}
});
}