下面列出了java.awt.FileDialog#SAVE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void showNativeDialog() {
String dirname = fd.getDirectory();
// File path has a priority against directory path.
String filename = fd.getFile();
if (filename != null) {
final File file = new File(filename);
if (fd.getMode() == FileDialog.LOAD
&& dirname != null
&& file.getParent() == null) {
// File path for gtk_file_chooser_set_filename.
filename = dirname + (dirname.endsWith(File.separator) ? "" :
File.separator) + filename;
}
if (fd.getMode() == FileDialog.SAVE && file.getParent() != null) {
// Filename for gtk_file_chooser_set_current_name.
filename = file.getName();
// Directory path for gtk_file_chooser_set_current_folder.
dirname = file.getParent();
}
}
run(fd.getTitle(), fd.getMode(), dirname, filename,
fd.getFilenameFilter(), fd.isMultipleMode(), fd.getX(), fd.getY());
}
private void showNativeDialog() {
String dirname = fd.getDirectory();
// File path has a priority against directory path.
String filename = fd.getFile();
if (filename != null) {
final File file = new File(filename);
if (fd.getMode() == FileDialog.LOAD
&& dirname != null
&& file.getParent() == null) {
// File path for gtk_file_chooser_set_filename.
filename = dirname + (dirname.endsWith(File.separator) ? "" :
File.separator) + filename;
}
if (fd.getMode() == FileDialog.SAVE && file.getParent() != null) {
// Filename for gtk_file_chooser_set_current_name.
filename = file.getName();
// Directory path for gtk_file_chooser_set_current_folder.
dirname = file.getParent();
}
}
GtkFileDialogPeer.this.run(fd.getTitle(), fd.getMode(), dirname,
filename, fd.getFilenameFilter(),
fd.isMultipleMode(), fd.getX(), fd.getY());
}
private void showNativeDialog() {
String dirname = fd.getDirectory();
// File path has a priority against directory path.
String filename = fd.getFile();
if (filename != null) {
final File file = new File(filename);
if (fd.getMode() == FileDialog.LOAD
&& dirname != null
&& file.getParent() == null) {
// File path for gtk_file_chooser_set_filename.
filename = dirname + (dirname.endsWith(File.separator) ? "" :
File.separator) + filename;
}
if (fd.getMode() == FileDialog.SAVE && file.getParent() != null) {
// Filename for gtk_file_chooser_set_current_name.
filename = file.getName();
// Directory path for gtk_file_chooser_set_current_folder.
dirname = file.getParent();
}
}
run(fd.getTitle(), fd.getMode(), dirname, filename,
fd.getFilenameFilter(), fd.isMultipleMode(), fd.getX(), fd.getY());
}
private void showNativeDialog() {
String dirname = fd.getDirectory();
// File path has a priority against directory path.
String filename = fd.getFile();
if (filename != null) {
final File file = new File(filename);
if (fd.getMode() == FileDialog.LOAD
&& dirname != null
&& file.getParent() == null) {
// File path for gtk_file_chooser_set_filename.
filename = dirname + (dirname.endsWith(File.separator) ? "" :
File.separator) + filename;
}
if (fd.getMode() == FileDialog.SAVE && file.getParent() != null) {
// Filename for gtk_file_chooser_set_current_name.
filename = file.getName();
// Directory path for gtk_file_chooser_set_current_folder.
dirname = file.getParent();
}
}
run(fd.getTitle(), fd.getMode(), dirname, filename,
fd.getFilenameFilter(), fd.isMultipleMode(), fd.getX(), fd.getY());
}
private void showNativeDialog() {
String dirname = fd.getDirectory();
// File path has a priority against directory path.
String filename = fd.getFile();
if (filename != null) {
final File file = new File(filename);
if (fd.getMode() == FileDialog.LOAD
&& dirname != null
&& file.getParent() == null) {
// File path for gtk_file_chooser_set_filename.
filename = dirname + (dirname.endsWith(File.separator) ? "" :
File.separator) + filename;
}
if (fd.getMode() == FileDialog.SAVE && file.getParent() != null) {
// Filename for gtk_file_chooser_set_current_name.
filename = file.getName();
// Directory path for gtk_file_chooser_set_current_folder.
dirname = file.getParent();
}
}
run(fd.getTitle(), fd.getMode(), dirname, filename,
fd.getFilenameFilter(), fd.isMultipleMode(), fd.getX(), fd.getY());
}
/**
*
*
* @param title
* @param mode
* @param filter
*
* @return
*/
private static File chooseFileAWT(String title, Mode mode, FileFilter filter) {
Easik e = Easik.getInstance();
EasikSettings s = e.getSettings();
FileDialog dialog = new FileDialog(e.getFrame(), title,
(mode == Mode.SAVE) ? FileDialog.SAVE : FileDialog.LOAD);
dialog.setDirectory(s.getDefaultFolder());
if ((mode == Mode.DIRECTORY) && EasikConstants.RUNNING_ON_MAC) {
System.setProperty("apple.awt.fileDialogForDirectories", "true");
} else if (filter != null) {
dialog.setFilenameFilter(filter);
}
// Show the dialog (this blocks until the user is done)
dialog.setVisible(true);
if ((mode == Mode.DIRECTORY) && EasikConstants.RUNNING_ON_MAC) {
System.setProperty("apple.awt.fileDialogForDirectories", "false");
}
String filename = dialog.getFile();
if (filename == null) {
return null;
}
File selected = new File(dialog.getDirectory(), filename);
if (mode != Mode.DIRECTORY) {
s.setProperty("folder_last", selected.getParentFile().getAbsolutePath());
}
return selected;
}
/**
* Opens a save dialog box when the user selects "Save As" from the menu.
*/
public void actionPerformed(ActionEvent e) {
FileDialog chooser = new FileDialog(frame,
"Use a .png or .jpg extension", FileDialog.SAVE);
chooser.setVisible(true);
if (chooser.getFile() != null) {
save(chooser.getDirectory() + File.separator + chooser.getFile());
}
}
public LinuxBrowseListener(BON2Gui parent, boolean isOpen, JTextField field) {
this.parent = parent;
this.isOpen = isOpen;
this.field = field;
if (isOpen) {
this.fd = new FileDialog(this.parent, "Please Select an Input File", FileDialog.LOAD);
} else {
this.fd = new FileDialog(this.parent, "Please Select an Output File", FileDialog.SAVE);
}
fd.setFilenameFilter( (dir, name) -> name.toLowerCase().endsWith(".jar"));
String key = isOpen ? BON2Gui.PREFS_KEY_OPEN_LOC : BON2Gui.PREFS_KEY_SAVE_LOC;
String savedDir = parent.prefs.get(key, Paths.get("").toAbsolutePath().toString());
File currentDir = new File(savedDir);
if (!Paths.get(savedDir).getRoot().toFile().exists()) {
currentDir = Paths.get("").toAbsolutePath().toFile();
}
while (!currentDir.isDirectory()) {
currentDir = currentDir.getParentFile();
}
fd.setDirectory(currentDir.getPath());
}
@Override
public String rChooseFile(Rengine re, int newFile) {
FileDialog fd = new FileDialog(Wandora.getWandora(), (newFile==0)?"Select a file":"Select a new file", (newFile==0)?FileDialog.LOAD:FileDialog.SAVE);
fd.setVisible(true);
String res=null;
if (fd.getDirectory()!=null) res=fd.getDirectory();
if (fd.getFile()!=null) res=(res==null)?fd.getFile():(res+fd.getFile());
return res;
}
/**
* Save the jMusic score as a standard MIDI file
* Prompt user for a filename
* @param Score
*/
public static void midi(Score score) {
FileDialog fd = new FileDialog(new Frame(),
"Save as a MIDI file ...",
FileDialog.SAVE);
fd.setFile("jMusic_composition.mid");
fd.show();
if (fd.getFile() != null) {
Write.midi(score, fd.getDirectory() + fd.getFile());
}
}
boolean initFile(){
MesquiteFileDialog fdlg= new MesquiteFileDialog(containerOfModule(), "Output File for Tree(s)", FileDialog.SAVE);
fdlg.setBackground(ColorTheme.getInterfaceBackground());
fdlg.setVisible(true);
String fileName=fdlg.getFile();
String directory=fdlg.getDirectory();
if (StringUtil.blank(fileName) || StringUtil.blank(directory))
return false;
saveFile = MesquiteFile.composePath(directory, fileName);
MesquiteFile.putFileContents(saveFile, "#NEXUS" + StringUtil.lineEnding(), true);
MesquiteFile.appendFileContents(saveFile, "BEGIN TREES;" + StringUtil.lineEnding(), true);
return true;
}
public static void saveAsFile(String filePathFull, String fileName) {
try {
File f=new File(filePathFull);
if(f.exists()){
if (filePathFull == null || filePathFull.trim().length() == 0)
return;
FileDialog fd = new FileDialog(new Frame(), "文件另存为",
FileDialog.SAVE);
fd.setFile(fileName);
//fd.setIconImage(SparkManager.getMainWindow().getIconImage());
fd.toFront();
fd.setVisible(true);
if (fd.getFile() != null) {
copyFile(new File(filePathFull), new File(fd.getDirectory()),fd.getFile());
} else {
fd.setAlwaysOnTop(false);
}
}else{
JOptionPane.showMessageDialog(null, Res.FILE_NOT_FOUND, "提示", 1);
}
} catch (Exception ex) {
log.error(ex.getMessage());
}
}
private void doSaveAs () {
FileDialog fileDialog = new FileDialog (this, "Save As...", FileDialog.SAVE);
fileDialog.show ();
if (fileDialog.getFile () == null)
return;
fileName = fileDialog.getDirectory () + File.separator + fileDialog.getFile ();
doSave (fileName);
}
/**
* Dialog to save score as a jMusic serialized jm file.
*/
public void saveJM() {
FileDialog fd = new FileDialog(this, "Save as a jm file...",FileDialog.SAVE);
fd.show();
//write a MIDI file to disk
if ( fd.getFile() != null) {
Write.jm(score, fd.getDirectory()+fd.getFile());
}
}
private void processDialog(int selectionMode, String last_dir, String title, int mode, Object[] filters,
Object[] result) {
JFileChooser fchooser = new JFileChooser();
File fileChoosen = null;
FileFilter filterChoosen = null;
if (!last_dir.isEmpty()) {
fchooser.setCurrentDirectory(new File(last_dir));
}
fchooser.setSelectedFile(null);
fchooser.setDialogTitle(title);
String btnApprove = "Select";
if (fromPopFile) {
fchooser.setFileSelectionMode(DIRSANDFILES);
fchooser.setAcceptAllFileFilterUsed(true);
} else {
fchooser.setFileSelectionMode(selectionMode);
if (mode == FileDialog.SAVE) {
fchooser.setDialogType(JFileChooser.SAVE_DIALOG);
btnApprove = "Save";
}
if (filters.length == 0) {
fchooser.setAcceptAllFileFilterUsed(true);
} else {
fchooser.setAcceptAllFileFilterUsed(false);
for (Object filter : filters) {
fchooser.setFileFilter((FileFilter) filter);
}
}
}
if (Settings.isMac()) {
fchooser.putClientProperty("JFileChooser.packageIsTraversable", "always");
}
int dialogResponse = fchooser.showDialog(parentFrame, btnApprove);
if (dialogResponse != JFileChooser.APPROVE_OPTION) {
fileChoosen = null;
} else {
fileChoosen = fchooser.getSelectedFile();
}
result[0] = fileChoosen;
if (filters.length > 0) {
result[1] = fchooser.getFileFilter();
}
}
/**
* perform SAVE AS
*/
void displaySaveAsDialog(int nextEvent) {
// pop up a dialog box for the user to enter a filename.
FileDialog fd = new FileDialog
(tw, PolicyTool.getMessage("Save.As"), FileDialog.SAVE);
fd.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
e.getWindow().setVisible(false);
}
});
fd.setVisible(true);
// see if the user hit cancel
if (fd.getFile() == null ||
fd.getFile().equals(""))
return;
// get the entered filename
File saveAsFile = new File(fd.getDirectory(), fd.getFile());
String filename = saveAsFile.getPath();
fd.dispose();
try {
// save the policy entries to a file
tool.savePolicy(filename);
// display status
MessageFormat form = new MessageFormat(PolicyTool.getMessage
("Policy.successfully.written.to.filename"));
Object[] source = {filename};
tw.displayStatusDialog(null, form.format(source));
// display the new policy filename
JTextField newFilename = (JTextField)tw.getComponent
(ToolWindow.MW_FILENAME_TEXTFIELD);
newFilename.setText(filename);
tw.setVisible(true);
// now continue with the originally requested command
// (QUIT, NEW, or OPEN)
userSaveContinue(tool, tw, this, nextEvent);
} catch (FileNotFoundException fnfe) {
if (filename == null || filename.equals("")) {
tw.displayErrorDialog(null, new FileNotFoundException
(PolicyTool.getMessage("null.filename")));
} else {
tw.displayErrorDialog(null, fnfe);
}
} catch (Exception ee) {
tw.displayErrorDialog(null, ee);
}
}
/**
* perform SAVE AS
*/
void displaySaveAsDialog(int nextEvent) {
// pop up a dialog box for the user to enter a filename.
FileDialog fd = new FileDialog
(tw, PolicyTool.getMessage("Save.As"), FileDialog.SAVE);
fd.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
e.getWindow().setVisible(false);
}
});
fd.setVisible(true);
// see if the user hit cancel
if (fd.getFile() == null ||
fd.getFile().equals(""))
return;
// get the entered filename
File saveAsFile = new File(fd.getDirectory(), fd.getFile());
String filename = saveAsFile.getPath();
fd.dispose();
try {
// save the policy entries to a file
tool.savePolicy(filename);
// display status
MessageFormat form = new MessageFormat(PolicyTool.getMessage
("Policy.successfully.written.to.filename"));
Object[] source = {filename};
tw.displayStatusDialog(null, form.format(source));
// display the new policy filename
JTextField newFilename = (JTextField)tw.getComponent
(ToolWindow.MW_FILENAME_TEXTFIELD);
newFilename.setText(filename);
tw.setVisible(true);
// now continue with the originally requested command
// (QUIT, NEW, or OPEN)
userSaveContinue(tool, tw, this, nextEvent);
} catch (FileNotFoundException fnfe) {
if (filename == null || filename.equals("")) {
tw.displayErrorDialog(null, new FileNotFoundException
(PolicyTool.getMessage("null.filename")));
} else {
tw.displayErrorDialog(null, fnfe);
}
} catch (Exception ee) {
tw.displayErrorDialog(null, ee);
}
}
/**
* perform SAVE AS
*/
void displaySaveAsDialog(int nextEvent) {
// pop up a dialog box for the user to enter a filename.
FileDialog fd = new FileDialog
(tw, PolicyTool.getMessage("Save.As"), FileDialog.SAVE);
fd.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
e.getWindow().setVisible(false);
}
});
fd.setVisible(true);
// see if the user hit cancel
if (fd.getFile() == null ||
fd.getFile().equals(""))
return;
// get the entered filename
File saveAsFile = new File(fd.getDirectory(), fd.getFile());
String filename = saveAsFile.getPath();
fd.dispose();
try {
// save the policy entries to a file
tool.savePolicy(filename);
// display status
MessageFormat form = new MessageFormat(PolicyTool.getMessage
("Policy.successfully.written.to.filename"));
Object[] source = {filename};
tw.displayStatusDialog(null, form.format(source));
// display the new policy filename
JTextField newFilename = (JTextField)tw.getComponent
(ToolWindow.MW_FILENAME_TEXTFIELD);
newFilename.setText(filename);
tw.setVisible(true);
// now continue with the originally requested command
// (QUIT, NEW, or OPEN)
userSaveContinue(tool, tw, this, nextEvent);
} catch (FileNotFoundException fnfe) {
if (filename == null || filename.equals("")) {
tw.displayErrorDialog(null, new FileNotFoundException
(PolicyTool.getMessage("null.filename")));
} else {
tw.displayErrorDialog(null, fnfe);
}
} catch (Exception ee) {
tw.displayErrorDialog(null, ee);
}
}
/**
* perform SAVE AS
*/
void displaySaveAsDialog(int nextEvent) {
// pop up a dialog box for the user to enter a filename.
FileDialog fd = new FileDialog
(tw, PolicyTool.getMessage("Save.As"), FileDialog.SAVE);
fd.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
e.getWindow().setVisible(false);
}
});
fd.setVisible(true);
// see if the user hit cancel
if (fd.getFile() == null ||
fd.getFile().equals(""))
return;
// get the entered filename
File saveAsFile = new File(fd.getDirectory(), fd.getFile());
String filename = saveAsFile.getPath();
fd.dispose();
try {
// save the policy entries to a file
tool.savePolicy(filename);
// display status
MessageFormat form = new MessageFormat(PolicyTool.getMessage
("Policy.successfully.written.to.filename"));
Object[] source = {filename};
tw.displayStatusDialog(null, form.format(source));
// display the new policy filename
JTextField newFilename = (JTextField)tw.getComponent
(ToolWindow.MW_FILENAME_TEXTFIELD);
newFilename.setText(filename);
tw.setVisible(true);
// now continue with the originally requested command
// (QUIT, NEW, or OPEN)
userSaveContinue(tool, tw, this, nextEvent);
} catch (FileNotFoundException fnfe) {
if (filename == null || filename.equals("")) {
tw.displayErrorDialog(null, new FileNotFoundException
(PolicyTool.getMessage("null.filename")));
} else {
tw.displayErrorDialog(null, fnfe);
}
} catch (Exception ee) {
tw.displayErrorDialog(null, ee);
}
}
/**
* perform SAVE AS
*/
void displaySaveAsDialog(int nextEvent) {
// pop up a dialog box for the user to enter a filename.
FileDialog fd = new FileDialog
(tw, PolicyTool.getMessage("Save.As"), FileDialog.SAVE);
fd.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
e.getWindow().setVisible(false);
}
});
fd.setVisible(true);
// see if the user hit cancel
if (fd.getFile() == null ||
fd.getFile().equals(""))
return;
// get the entered filename
File saveAsFile = new File(fd.getDirectory(), fd.getFile());
String filename = saveAsFile.getPath();
fd.dispose();
try {
// save the policy entries to a file
tool.savePolicy(filename);
// display status
MessageFormat form = new MessageFormat(PolicyTool.getMessage
("Policy.successfully.written.to.filename"));
Object[] source = {filename};
tw.displayStatusDialog(null, form.format(source));
// display the new policy filename
JTextField newFilename = (JTextField)tw.getComponent
(ToolWindow.MW_FILENAME_TEXTFIELD);
newFilename.setText(filename);
tw.setVisible(true);
// now continue with the originally requested command
// (QUIT, NEW, or OPEN)
userSaveContinue(tool, tw, this, nextEvent);
} catch (FileNotFoundException fnfe) {
if (filename == null || filename.equals("")) {
tw.displayErrorDialog(null, new FileNotFoundException
(PolicyTool.getMessage("null.filename")));
} else {
tw.displayErrorDialog(null, fnfe);
}
} catch (Exception ee) {
tw.displayErrorDialog(null, ee);
}
}