下面列出了javax.swing.JFileChooser#showDialog ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Open a file.
*/
protected void openFile() {
JFileChooser jc = new JFileChooser();
String pathName = userPreferences.getDiskImageDirectory();
if (null == pathName) {
pathName = ""; //$NON-NLS-1$
}
jc.setCurrentDirectory(new File(pathName));
EmulatorFileFilter ff = new EmulatorFileFilter();
jc.setFileFilter(ff);
int rc = jc.showDialog(this, textBundle.get("Open")); //$NON-NLS-1$
if (rc == 0) {
userPreferences.setDiskImageDirectory(jc.getSelectedFile().getParent());
UserPreferences.getInstance().save();
addDiskExplorerTab(jc.getSelectedFile());
}
}
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(false);
String currentPath = txtFilename.getText();
if (currentPath.length() > 0) {
File currentFile = new File(currentPath);
File currentDir = currentFile.getParentFile();
if (currentDir != null && currentDir.exists())
fileChooser.setCurrentDirectory(currentDir);
}
int returnVal = fileChooser.showDialog(MZmineCore.getDesktop().getMainWindow(), "Select file");
if (returnVal == JFileChooser.APPROVE_OPTION) {
String selectedPath = fileChooser.getSelectedFile().getAbsolutePath();
txtFilename.setText(selectedPath);
}
}
private void browsePressed() {
JFileChooser chooser = FileChooserFactory.getPkcs12FileChooser();
File currentExportFile = new File(jtfExportFile.getText().trim());
if ((currentExportFile.getParentFile() != null) && (currentExportFile.getParentFile().exists())) {
chooser.setCurrentDirectory(currentExportFile.getParentFile());
chooser.setSelectedFile(currentExportFile);
} else {
chooser.setCurrentDirectory(CurrentDirectory.get());
}
chooser.setDialogTitle(res.getString("DExportKeyPair.ChooseExportFile.Title"));
chooser.setMultiSelectionEnabled(false);
int rtnValue = JavaFXFileChooser.isFxAvailable() ? chooser.showSaveDialog(this)
: chooser.showDialog(this, res.getString("DExportKeyPair.ChooseExportFile.button"));
if (rtnValue == JFileChooser.APPROVE_OPTION) {
File chosenFile = chooser.getSelectedFile();
CurrentDirectory.updateForFile(chosenFile);
jtfExportFile.setText(chosenFile.toString());
jtfExportFile.setCaretPosition(0);
}
}
private void browsePressed() {
JFileChooser chooser = FileChooserFactory.getLibFileChooser();
File currentLibFile = new File(((String) jtfP11Library.getSelectedItem()).trim());
if (currentLibFile.getParentFile() != null && currentLibFile.getParentFile().exists()) {
chooser.setCurrentDirectory(currentLibFile.getParentFile());
chooser.setSelectedFile(currentLibFile);
} else {
chooser.setCurrentDirectory(CurrentDirectory.get());
}
chooser.setDialogTitle(res.getString("DOpenPkcs11KeyStore.SelectLib.Title"));
chooser.setMultiSelectionEnabled(false);
int rtnValue = chooser.showDialog(this, res.getString("DOpenPkcs11KeyStore.SelectLib.button"));
if (rtnValue == JFileChooser.APPROVE_OPTION) {
File chosenFile = chooser.getSelectedFile();
CurrentDirectory.updateForFile(chosenFile);
String libPath = chosenFile.toString();
jtfP11Library.addItem(libPath);
jtfP11Library.setSelectedItem(libPath);
}
}
private void certificateBrowsePressed() {
JFileChooser chooser = FileChooserFactory.getCertFileChooser();
File currentFile = new File(jtfCertificatePath.getText());
if ((currentFile.getParentFile() != null) && (currentFile.getParentFile().exists())) {
chooser.setCurrentDirectory(currentFile.getParentFile());
} else {
chooser.setCurrentDirectory(CurrentDirectory.get());
}
chooser.setDialogTitle(res.getString("DImportKeyPairPvk.ChooseCertificate.Title"));
chooser.setMultiSelectionEnabled(false);
int rtnValue = chooser.showDialog(this, res.getString("DImportKeyPairPvk.CertificateFileChooser.button"));
if (rtnValue == JFileChooser.APPROVE_OPTION) {
File chosenFile = chooser.getSelectedFile();
CurrentDirectory.updateForFile(chosenFile);
jtfCertificatePath.setText(chosenFile.toString());
jtfCertificatePath.setCaretPosition(0);
}
}
public void setFilePath(JFormattedTextField textField){
//begin with creation of new file
JFileChooser fc = new JFileChooser();
//set directory and ".log" filter
fc.setCurrentDirectory(new File(System.getProperty("user.dir")));
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setFileFilter(logFileFilter_);
int status = fc.showDialog(this, Messages.getString("EditLogControlPanel.approveButton"));
if(status == JFileChooser.APPROVE_OPTION){
textField.setValue(fc.getSelectedFile().getAbsolutePath());
textField.setToolTipText(fc.getSelectedFile().getAbsolutePath());
textField.setCaretPosition(textField.getText().length());
}
}
private static File[] getFilePath(Component component, String title) {
File[] files = null;
final File openDir = new File(SnapApp.getDefault().getPreferences().
get(OpenProductAction.PREFERENCES_KEY_LAST_PRODUCT_DIR, "."));
final JFileChooser chooser = FileChooserFactory.getInstance().createFileChooser(openDir);
chooser.setMultiSelectionEnabled(true);
chooser.setDialogTitle(title);
if (chooser.showDialog(component, "OK") == JFileChooser.APPROVE_OPTION) {
files = chooser.getSelectedFiles();
SnapApp.getDefault().getPreferences().
put(OpenProductAction.PREFERENCES_KEY_LAST_PRODUCT_DIR, chooser.getCurrentDirectory().getAbsolutePath());
}
return files;
}
protected void batchCreate() {
final JFileChooser batchDialog = new JFileChooser(Preferences.getInstance().getSavePath());
batchDialog.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// final FileDialog saveDialog = new FileDialog(this);
batchDialog.setDialogType(JFileChooser.OPEN_DIALOG);
batchDialog.setDialogTitle("Choose the base directory to browse recursively");
batchDialog.showDialog(this, "Batch Create Program");
final File dir = batchDialog.getSelectedFile();
if (dir != null) {
try {
programEditor.batchCreate(dir);
} catch (Exception e) {
e.printStackTrace();// error occurred
}
}
}
/**
* Opens dialog window to select desired PEAR package file for a given component.
*
* @param componentId
* The given component ID.
* @return Selected PEAR package file for the given component, or <code>null</code>, if the
* selection cancelled.
*/
public synchronized File selectPackageFile(String componentId) {
// get last package file path
Preferences userPrefs = Preferences.userNodeForPackage(getClass());
String lastFilePath = (userPrefs != null) ? userPrefs.get(LAST_PACKAGE_FILE_KEY, "") : "";
File lastFile = lastFilePath.length() > 0 ? new File(lastFilePath) : null;
File lastDir = lastFile != null ? lastFile.getParentFile() : new File(".");
// create JFileChooser
JFileChooser fileChooser = new JFileChooser();
fileChooser.addChoosableFileFilter(new PackageFileFilter());
fileChooser.setCurrentDirectory(lastDir);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setDialogTitle("Select " + componentId + " PEAR file");
// show information dialog
String message = "Select the PEAR file of the\n" + "\n" + componentId + "\n"
+ "\ncomponent using the following file dialog.";
JOptionPane.showMessageDialog(_dialogFrame, message);
// open dialog window
File selectedFile = null;
int result = fileChooser.showDialog(_dialogFrame, "Select");
if (result == JFileChooser.APPROVE_OPTION) {
// set 'selected file'
selectedFile = fileChooser.getSelectedFile();
if (selectedFile != null) {
// set 'last file' in user prefs
userPrefs.put(LAST_PACKAGE_FILE_KEY, selectedFile.getAbsolutePath());
}
}
return selectedFile;
}
/**
* Selects the tabset with a chooser.
*/
private void chooseTabSet() {
String fileName = XML.getResolvedPath(builder.tabSetName, Launcher.tabSetBasePath);
JFileChooser chooser = Launcher.getXMLChooser();
chooser.setSelectedFile(new File(fileName));
chooser.setFileFilter(Launcher.xsetFileFilter);
int result = chooser.showDialog(this, LaunchRes.getString("Saver.FileChooser.Title")); //$NON-NLS-1$
if(result==JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
fileName = file.getAbsolutePath();
setTabSetName(fileName);
setTabSetBasePath(XML.getDirectoryPath(fileName));
}
refresh();
}
/**
*
*
* @param title
* @param mode
* @param filter
*
* @return
*/
private static File chooseFileSwing(String title, Mode mode, FileFilter filter, String defaultName) {
Easik e = Easik.getInstance();
EasikSettings s = e.getSettings();
JFileChooser dialog = new JFileChooser(s.getDefaultFolder());
if (filter != null) {
dialog.setFileFilter(filter);
}
int result;
if (mode == Mode.DIRECTORY) {
dialog.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
result = dialog.showDialog(e.getFrame(), "Select folder");
} else if (mode == Mode.SAVE) {
if (defaultName != null) {
dialog.setSelectedFile(new File(defaultName));
}
result = dialog.showSaveDialog(e.getFrame());
} else {
result = dialog.showOpenDialog(e.getFrame());
}
if (result != JFileChooser.APPROVE_OPTION) {
return null;
}
File selFile = dialog.getSelectedFile();
if (mode != Mode.DIRECTORY) {
s.setProperty("folder_last", selFile.getParentFile().getAbsolutePath());
}
return selFile;
}
private Path getExportPath() {
Path file = null;
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new PNGFileFilter());
fileChooser.setAcceptAllFileFilterUsed(false);
fileChooser.setSelectedFile(Paths.get(pathField.getText()).toAbsolutePath().toFile());
if (fileChooser.showDialog(dialog, "Confirm") == JFileChooser.APPROVE_OPTION) {
file = Actions.appendFileExtensionIfNecessary(fileChooser.getSelectedFile().toPath(), "png");
}
return file;
}
public void actionPerformed (ActionEvent e) {
if (!listen) return;
Object o = e.getSource ();
if (o == cbAlwaysShowOutput) {
fireChanged();
} else
if (o == cbReuseOutput) {
fireChanged();
} else
if (o == cbSaveFiles) {
fireChanged();
} else
if (o == cbVerbosity) {
fireChanged();
} else
if (o == bAntHome) {
JFileChooser chooser = new JFileChooser (tfAntHome.getText ());
chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
int r = chooser.showDialog (
SwingUtilities.getWindowAncestor (this),
NbBundle.getMessage(AntCustomizer.class, "Select_Directory")
);
if (r == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile ();
if (!new File (new File (file, "lib"), "ant.jar").isFile ()) {
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
NbBundle.getMessage(AntCustomizer.class, "Not_a_ant_home", file),
NotifyDescriptor.Message.WARNING_MESSAGE
));
return;
}
tfAntHome.setText (file.getAbsolutePath ());
AntSettings.setAntHome(file);
updateAntVersion();
fireChanged();
}
}
}
/**
* Deploy module from directory on GlassFish server.
* <p/>
* @return Result of undeploy task execution.
*/
@Override
public Future<ResultString> deployDirectory() {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(NbBundle.getMessage(Hk2ItemNode.class,
"LBL_ChooseButton"));
chooser.setDialogType(JFileChooser.CUSTOM_DIALOG);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setMultiSelectionEnabled(false);
int returnValue = chooser.showDialog(WindowManager.getDefault()
.getMainWindow(), NbBundle.getMessage(
Hk2ItemNode.class, "LBL_ChooseButton"));
if (instance != null
|| returnValue != JFileChooser.APPROVE_OPTION) {
return null;
}
final File dir
= new File(chooser.getSelectedFile().getAbsolutePath());
Future<ResultString> future = ServerAdmin.<ResultString>exec(
instance, new CommandDeploy(dir.getParentFile().getName(),
Util.computeTarget(instance.getProperties()),
dir, null, null, null));
status = new WeakReference<Future<ResultString>>(future);
return future;
}
/**
* Opens the file chooser for loading the current configuration file.
*/
public void load() {
JFileChooser fc = fileChooser.get();
fc.setFileFilter(null);
fc.setVisible(true);
fc.setCurrentDirectory(getFile());
int returnVal = fc.showDialog(null, "Load");
if (returnVal == JFileChooser.APPROVE_OPTION) {
load(fc.getSelectedFile());
}
}
public void actionPerformed(ActionEvent e) {
JFileChooser jfc=new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.showDialog(new JLabel(), "Select");
File file=jfc.getSelectedFile();
if(file.isDirectory()){
logger.debug("Folder:"+file.getAbsolutePath());
}else if(file.isFile()){
System.out.println("File:"+file.getAbsolutePath());
}
logger.debug(jfc.getSelectedFile().getName());
}
private void pickScanReportFile() {
JFileChooser fileChooser = new JFileChooser(new File(folderField.getText()));
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int returnVal = fileChooser.showDialog(frame, "Select scan report file");
if (returnVal == JFileChooser.APPROVE_OPTION)
scanReportFileField.setText(fileChooser.getSelectedFile().getAbsolutePath());
}
@Override
@Nullable
public File msgSaveFileDialog(@Nullable final Component parentComponent, @Nonnull final String id, @Nonnull final String title, @Nullable final File defaultFolder, final boolean filesOnly, @Nonnull @MustNotContainNull final FileFilter[] fileFilters, @Nonnull final String approveButtonText) {
final File folderToUse;
if (defaultFolder == null) {
folderToUse = cacheSaveFileThroughDialog.find(null, id);
} else {
folderToUse = defaultFolder;
}
final JFileChooser fileChooser = new JFileChooser(folderToUse);
fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
fileChooser.setDialogTitle(title);
fileChooser.setApproveButtonText(approveButtonText);
fileChooser.setAcceptAllFileFilterUsed(true);
for (final FileFilter f : fileFilters) {
fileChooser.addChoosableFileFilter(f);
}
if (fileFilters.length != 0) {
fileChooser.setFileFilter(fileFilters[0]);
}
fileChooser.setMultiSelectionEnabled(false);
File result = null;
if (fileChooser.showDialog(GetUtils.ensureNonNull(
parentComponent == null ? null : SwingUtilities.windowForComponent(parentComponent),
Main.getApplicationFrame()),
approveButtonText) == JFileChooser.APPROVE_OPTION
) {
result = cacheSaveFileThroughDialog.put(id, fileChooser.getSelectedFile());
}
return result;
}
private void selectFile(JTextField field, String type) {
JFileChooser c = ResourceEditorView.createFileChooser("*" + type, type);
if(JFileChooser.APPROVE_OPTION == c.showDialog(this, "Select")) {
File f = c.getSelectedFile();
field.setText(f.getAbsolutePath());
Preferences.userNodeForPackage(ResourceEditorView.class).put("lastDir", f.getParentFile().getAbsolutePath());
}
updateMIDletList();
if(midletPicker.getModel().getSize() > 0) {
midletPicker.setSelectedIndex(0);
}
updateOKEnabled();
}
private void pickFolder() {
JFileChooser fileChooser = new JFileChooser(new File(folderField.getText()));
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = fileChooser.showDialog(frame, "Select folder");
if (returnVal == JFileChooser.APPROVE_OPTION) {
File selectedDirectory = fileChooser.getSelectedFile();
if (!selectedDirectory.exists()) {
// When no directory is selected when approving, FileChooser incorrectly appends the current directory to the path.
// Take the opened directory instead.
selectedDirectory = fileChooser.getCurrentDirectory();
}
folderField.setText(selectedDirectory.getAbsolutePath());
}
}