下面列出了java.awt.FileDialog#setMode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private File showFileDialog( FileDialog fileDialog, int mode ) {
String oldFileDialogProp = System.getProperty("apple.awt.fileDialogForDirectories"); //NOI18N
if( dirsOnly ) {
System.setProperty("apple.awt.fileDialogForDirectories", "true"); //NOI18N
}
fileDialog.setMode( mode );
fileDialog.setVisible(true);
if( dirsOnly ) {
if( null != oldFileDialogProp ) {
System.setProperty("apple.awt.fileDialogForDirectories", oldFileDialogProp); //NOI18N
} else {
System.clearProperty("apple.awt.fileDialogForDirectories"); //NOI18N
}
}
if( fileDialog.getDirectory() != null && fileDialog.getFile() != null ) {
String selFile = fileDialog.getFile();
File dir = new File( fileDialog.getDirectory() );
return new File( dir, selFile );
}
return null;
}
/**
* Displays a file chooser dialog for the user to select the file to which the current game should
* be saved.
*
* @param frame The owner of the file chooser dialog; may be {@code null}.
* @return The file to which the current game should be saved or {@code null} if the user
* cancelled the operation.
*/
public static File getSaveGameLocation(final Frame frame, final GameData gameData) {
final FileDialog fileDialog = new FileDialog(frame);
fileDialog.setMode(FileDialog.SAVE);
fileDialog.setDirectory(ClientSetting.saveGamesFolderPath.getValueOrThrow().toString());
fileDialog.setFilenameFilter((dir, name) -> GameDataFileUtils.isCandidateFileName(name));
fileDialog.setFile(getSaveGameName(gameData));
fileDialog.setVisible(true);
final String fileName = fileDialog.getFile();
if (fileName == null) {
return null;
}
// If the user selects a filename that already exists,
// the AWT Dialog will ask the user for confirmation
return new File(fileDialog.getDirectory(), GameDataFileUtils.addExtensionIfAbsent(fileName));
}
public void open() {
final FileDialog openDialog = new FileDialog(this);
openDialog.setDirectory(Preferences.getInstance().getOpenPath());
openDialog.setMode(FileDialog.LOAD);
openDialog.setFilenameFilter(new FilenameFilter() {
public boolean accept(File dir, String name) {
String[] supportedFiles = { "PGM", "pgm" };
for (int i = 0; i < supportedFiles.length; i++) {
if (name.endsWith(supportedFiles[i])) {
return true;
}
}
return false;
}
});
openDialog.setVisible(true);
Preferences.getInstance().setOpenPath(openDialog.getDirectory());
if (openDialog.getDirectory() != null && openDialog.getFile() != null) {
String filePath = openDialog.getDirectory() + openDialog.getFile();
System.out.println(filePath);
final File pgmFile = new File(filePath);
final MainFrame newFrame = new MainFrame(pgmFile);
newFrame.show();
}
}
@Override
public void exportAsNetbeansProject(Sample sample) {
FileDialog fileSelector = new FileDialog((JFrame)SwingUtilities.getWindowAncestor(view), "Select Destination");
fileSelector.setMode(FileDialog.SAVE);
fileSelector.setVisible(true);
String selectedFile = fileSelector.getFile();
if (selectedFile == null) {
return;
}
File f = new File(new File(fileSelector.getDirectory()), selectedFile);
System.out.println(f);
new Thread(()->{
try {
sample.exportAsNetbeansProject(ctx, f);
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(view, ex.getMessage(), "Export Failed", JOptionPane.ERROR_MESSAGE);
}
}).start();
}
public void openFile() {
JSONObject filesystem = Lizzie.config.persisted.getJSONObject("filesystem");
FileDialog fileDialog = new FileDialog(this, resourceBundle.getString("LizzieFrame.openFile"));
fileDialog.setLocationRelativeTo(this);
fileDialog.setDirectory(filesystem.getString("last-folder"));
fileDialog.setFile("*.sgf;*.gib;*.SGF;*.GIB");
fileDialog.setMultipleMode(false);
fileDialog.setMode(0);
fileDialog.setVisible(true);
File[] file = fileDialog.getFiles();
if (file.length > 0) loadFile(file[0]);
}
/** Invoker for the saveas action */
public void saveAs() {
Doc doc = getSelectedDoc();
if (doc == null) {
throw new NullPointerException ("no doc");
}
FileDialog fd = new FileDialog (MiniEdit.this, "Save as");
fd.setMode(fd.SAVE);
fd.show();
if (fd.getFile() != null) {
File nue = new File (fd.getDirectory() + fd.getFile());
try {
boolean success = nue.createNewFile();
if (success) {
FileWriter w = new FileWriter (nue);
doc.getTextPane().write(w);
file = nue;
documentTabs.setTitleAt(documentTabs.getSelectedIndex(), nue.getName());
documentTabs.setToolTipTextAt(documentTabs.getSelectedIndex(), nue.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void radioButtonChanged(boolean cDM, boolean cRK, boolean cOS, boolean cRS, boolean cCS) {
boolean oldRandomKey = randomKey;
dontModify = jwtST.getRdbtnDontModify().isSelected();
randomKey = jwtST.getRdbtnRandomKey().isSelected();
keepOriginalSignature = jwtST.getRdbtnOriginalSignature().isSelected();
recalculateSignature = jwtST.getRdbtnRecalculateSignature().isSelected();
chooseSignature = jwtST.getRdbtnChooseSignature().isSelected();
jwtST.setKeyFieldState(!keepOriginalSignature && !dontModify && !randomKey && !chooseSignature);
if (keepOriginalSignature || dontModify) {
jwtIM.setJWTKey("");
jwtST.setKeyFieldValue("");
}
if (randomKey && !oldRandomKey) {
generateRandomKey();
}
if (cCS) {
FileDialog dialog = new FileDialog((Frame) null, "Select File to Open");
dialog.setMode(FileDialog.LOAD);
dialog.setVisible(true);
if(dialog.getFile()!=null) {
String file = dialog.getDirectory() + dialog.getFile();
Output.output(file + " chosen.");
String chosen = Strings.filePathToString(file);
jwtIM.setJWTKey(chosen);
jwtST.updateSetView(false);
}
}
}
/**
* Returns files the user selected or an empty array if the user cancelled
* the dialog.
*/
public static File[] showOpenDialog(Frame f, String title,
boolean allowMultipleSelection, FilenameFilter filter) {
FileDialog fd = new FileDialog(f, title);
fd.setMode(FileDialog.LOAD);
if (filter != null)
fd.setFilenameFilter(filter);
fd.pack();
fd.setMultipleMode(allowMultipleSelection);
fd.setLocationRelativeTo(null);
fd.setVisible(true);
if (fd.getFile() == null)
return new File[] {};
return fd.getFiles();
}
/**
* Show a save FileDialog.
*
* @param f
* the frame that owns the FileDialog.
* @param title
* the dialog title
* @param filename
* the optional default filename shown in the file dialog.
* @param extension
* the file extension ("xml", "png", etc.)
* @return a File the user chose.
*/
public static File showSaveDialog(Frame f, String title, String filename,
String extension) {
if (extension.startsWith("."))
extension = extension.substring(1);
FileDialog fd = new FileDialog(f, title);
fd.setMode(FileDialog.SAVE);
if (filename != null)
fd.setFile(filename);
fd.setFilenameFilter(new SuffixFilenameFilter(extension));
fd.pack();
fd.setLocationRelativeTo(null);
fd.setVisible(true);
String s = fd.getFile();
if (s == null)
return null;
if (s.toLowerCase().endsWith("." + extension)) {
return new File(fd.getDirectory() + s);
}
// TODO: show a 'are you sure you want to replace' dialog here, if we
// change the filename
// native FileDialogs don't always show the right warning dialog IF the
// file extension
// isn't present
return new File(fd.getDirectory() + fd.getFile() + "." + extension);
}
private static int showNativeFileDialog(final JFileChooser chooser) {
final FileDialog result = new FileDialog((Frame) chooser.getParent());
result.setDirectory(chooser.getCurrentDirectory().getPath());
final File selected = chooser.getSelectedFile();
result.setFile(selected == null ? "" : selected.getPath());
result.setFilenameFilter(new FilenameFilter() {
@Override
public boolean accept(final File dir, final String name) {
return chooser.getFileFilter().accept(new File(dir.getPath() + File.pathSeparator + name));
}
});
if (chooser.getDialogType() == SAVE_DIALOG) {
result.setMode(FileDialog.SAVE);
} else {
// The native dialog only support Open and Save
result.setMode(FileDialog.LOAD);
}
if (chooser.getFileSelectionMode() == DIRECTORIES_ONLY) {
System.setProperty("apple.awt.fileDialogForDirectories", "true");
}
// Display dialog
result.setVisible(true);
System.setProperty("apple.awt.fileDialogForDirectories", "false");
if (result.getFile() == null) {
return CANCEL_OPTION;
}
final String selectedDir = result.getDirectory();
chooser
.setSelectedFile(new File(FileUtils.ensureTrailingSlash(selectedDir) + result.getFile()));
return APPROVE_OPTION;
}
/**
* Opens up a UI pop-up allowing user to select a game file. Returns nothing if user closes the
* pop-up.
*/
public Optional<File> selectGameFile(final Frame owner) {
final FileDialog fileDialog = new FileDialog(owner);
fileDialog.setMode(FileDialog.LOAD);
fileDialog.setDirectory(ClientSetting.saveGamesFolderPath.getValueOrThrow().toString());
fileDialog.setFilenameFilter((dir, name) -> GameDataFileUtils.isCandidateFileName(name));
fileDialog.setVisible(true);
// FileDialog.getFiles() always returns an array
// of 1 or 0 items, because FileDialog.multipleMode is false by default
return Arrays.stream(fileDialog.getFiles()).findAny().map(this::mapFileResult);
}
public void saveAs() {
final FileDialog saveDialog = new FileDialog(this);
saveDialog.setDirectory(Preferences.getInstance().getSavePath());
saveDialog.setMode(FileDialog.SAVE);
saveDialog.setFilenameFilter(new FilenameFilter() {
public boolean accept(File dir, String name) {
String[] supportedFiles = { "PGM", "pgm" };
for (int i = 0; i < supportedFiles.length; i++) {
if (name.endsWith(supportedFiles[i])) {
return true;
}
}
return false;
}
});
saveDialog.setVisible(true);
Preferences.getInstance().setSavePath(saveDialog.getDirectory());
String filename = saveDialog.getFile();
if (saveDialog.getDirectory() != null && filename != null) {
if (!filename.toUpperCase().endsWith(".PGM")) {
filename += ".PGM";
}
String filePath = saveDialog.getDirectory() + filename;
System.out.println(filePath);
final File file = new File(filePath);
setPgmFile(file);
program.save(pgmFile);
}
}
private File chooseFile() {
JFrame mainFrame = (JFrame) WindowManager.getDefault().getMainWindow();
FileDialog fileDialog = new FileDialog(mainFrame, new File(System.getProperty("user.home")).toString());
fileDialog.setTitle("Export changes in cvs file");
fileDialog.setFile("expected.csv");
fileDialog.setMode(FileDialog.SAVE);
fileDialog.setFilenameFilter(new ExtFileFilter("csv"));
fileDialog.setVisible(true);
String filename = fileDialog.getFile();
if (filename == null){
return null;
}
String dir = fileDialog.getDirectory();
return new File(dir + File.separator + filename);
}
/**
* {@inheritDoc} Displays a file chooser dialog
* and opens the selected files.
*/
@Override
public void actionPerformed(ActionEvent e) {
if (running) {
return;
}
try {
running = true;
JFileChooser chooser = prepareFileChooser();
File[] files;
try {
if( Boolean.getBoolean("nb.native.filechooser") ) { //NOI18N
String oldFileDialogProp = System.getProperty("apple.awt.fileDialogForDirectories"); //NOI18N
System.setProperty("apple.awt.fileDialogForDirectories", "false"); //NOI18N
FileDialog fileDialog = new FileDialog(WindowManager.getDefault().getMainWindow());
fileDialog.setMode(FileDialog.LOAD);
fileDialog.setDirectory(getCurrentDirectory().getAbsolutePath());
fileDialog.setTitle(chooser.getDialogTitle());
fileDialog.setVisible(true);
if( null != oldFileDialogProp ) {
System.setProperty("apple.awt.fileDialogForDirectories", oldFileDialogProp); //NOI18N
} else {
System.clearProperty("apple.awt.fileDialogForDirectories"); //NOI18N
}
if( fileDialog.getDirectory() != null && fileDialog.getFile() != null ) {
String selFile = fileDialog.getFile();
File dir = new File( fileDialog.getDirectory() );
files = new File[] { new File( dir, selFile ) };
currentDirectory = dir;
} else {
throw new UserCancelException();
}
} else {
files = chooseFilesToOpen(chooser);
currentDirectory = chooser.getCurrentDirectory();
if (chooser.getFileFilter() != null) { // #227187
currentFileFilter =
chooser.getFileFilter().getDescription();
}
}
} catch (UserCancelException ex) {
return;
}
for (int i = 0; i < files.length; i++) {
OpenFile.openFile(files[i], -1);
}
} finally {
running = false;
}
}