下面列出了java.awt.FileDialog#getFile ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void doLoadDisk()
{
FileDialog fd = new FileDialog(getFrame(), "Load Script", FileDialog.LOAD);
fd.setDirectory(RuntimeLogic.getProp("script.file.dir"));
fd.setFile(RuntimeLogic.getProp("script.file.file"));
fd.setVisible(true);
if (fd.getDirectory() == null)
return;
String historyFile = fd.getDirectory()+System.getProperty("file.separator")+fd.getFile();
if ((historyFile == null) || (historyFile.length() == 0))
return;
try
{
ScriptLogic.load(mRuntime, new File(historyFile));
RuntimeLogic.setProp("script.file.dir", fd.getDirectory());
RuntimeLogic.setProp("script.file.file", fd.getFile());
}
catch (IOException e)
{
JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "Error reading "+historyFile, JOptionPane.ERROR_MESSAGE);
}
}
/**
* If invoked from within a Frame: this pulls up a FileDialog to browse for
* a file. If this is invoked from a secure applet: then this will throw an
* exception.
*
* @param ext
* an optional list of extensions
* @return a File, or null if the user declined to select anything.
*/
public File browseForFile(String... ext) {
Window w = SwingUtilities.getWindowAncestor(this);
if (!(w instanceof Frame))
throw new IllegalStateException();
Frame frame = (Frame) w;
FileDialog fd = new FileDialog(frame);
if (ext != null && ext.length > 0
&& (!(ext.length == 1 && ext[0] == null)))
fd.setFilenameFilter(new SuffixFilenameFilter(ext));
fd.pack();
fd.setLocationRelativeTo(null);
fd.setVisible(true);
String d = fd.getFile();
if (d == null)
return null;
return new File(fd.getDirectory() + fd.getFile());
}
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;
}
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();
}
}
private void doSave()
{
FileDialog fd = new FileDialog(getFrame(), "Save History File", FileDialog.SAVE);
fd.setDirectory(RuntimeLogic.getProp("history.file.dir"));
fd.setFile(RuntimeLogic.getProp("history.file.file"));
fd.setVisible(true);
if (fd.getDirectory() == null)
return;
String historyFile = fd.getDirectory()+System.getProperty("file.separator")+fd.getFile();
if ((historyFile == null) || (historyFile.length() == 0))
return;
try
{
RuntimeLogic.saveHistory(mRuntime, new File(historyFile));
RuntimeLogic.setProp("history.file.dir", fd.getDirectory());
RuntimeLogic.setProp("history.file.file", fd.getFile());
}
catch (IOException e)
{
JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "Error reading "+historyFile, JOptionPane.ERROR_MESSAGE);
}
}
public String rChooseFile(Rengine re, int newFile) {
FileDialog fd = new FileDialog(new Frame(),
(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;
}
/**
*
*
* @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());
}
}
/**
* 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();
}
/**
* This method cannot be called directly.
*/
@Override
public void actionPerformed(ActionEvent e) {
FileDialog chooser = new FileDialog(StdDraw.frame, "Use a .png or .jpg extension", FileDialog.SAVE);
chooser.setVisible(true);
String filename = chooser.getFile();
if (filename != null) {
StdDraw.save(chooser.getDirectory() + File.separator + chooser.getFile());
}
}
/**
* 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());
}
}
/**
* 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());
}
}
/**
* Import to a jMusic score a standard MIDI file
* Assume the MIDI file name is the same as the score title with .mid appended
* prompt for a fileName
* @param Score
*/
public static void midi(Score score) {
FileDialog fd = new FileDialog(new Frame(),
"Select a MIDI file to open.",
FileDialog.LOAD);
fd.show();
if (fd.getFile() != null) {
Read.midi(score, fd.getDirectory() + fd.getFile());
}
}
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;
}
/**
* Show a browse dialog, using the current file extensions if they are
* defined.
* <p>
* In a JNLP session this uses a FileOpenService, but otherwise this uses an
* AWT file dialog.
*
* @param component
* a component that relates to the Frame the possible dialog may
* be anchored to. This can be any component that is showing.
* @param extensions
* a list of possible extensions (or null if all files are
* accepted).
* @return the FileData the user selected, or null if the user cancelled
* this operation.
*/
public static FileData doBrowse(Component component, String[] extensions) {
/*
* if(JVM.isJNLP()) { try { FileOpenService fos =
* (FileOpenService)ServiceManager.lookup("javax.jnlp.FileOpenService");
* final FileContents contents = fos.openFileDialog(null, extensions);
* if(contents==null) return null; return new
* FileContentsWrapper(contents); } catch(Exception e) {
* e.printStackTrace(); } }
*/
Window w = SwingUtilities.getWindowAncestor(component);
if (w instanceof Frame) {
Frame f = (Frame) w;
FileDialog fd = new FileDialog(f);
String[] ext = extensions;
if (ext != null && ext.length > 0) {
fd.setFilenameFilter(new SuffixFilenameFilter(ext));
}
fd.pack();
fd.setLocationRelativeTo(null);
fd.setVisible(true);
if (fd.getFile() == null)
return null;
File file = new File(fd.getDirectory() + fd.getFile());
return new FileWrapper(file);
} else {
throw new RuntimeException("window ancestor: " + w);
}
}
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);
}
}
}
/**
* 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);
}
}