下面列出了java.awt.FileDialog#setVisible ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void doLoadDisk()
{
FileDialog fd = new FileDialog(getFrame(), "Load Test Suite", FileDialog.LOAD);
fd.setDirectory(RuntimeLogic.getProp("suite.file.dir"));
fd.setFile(RuntimeLogic.getProp("suite.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
{
SuiteLogic.load(mRuntime, new File(historyFile));
RuntimeLogic.setProp("suite.file.dir", fd.getDirectory());
RuntimeLogic.setProp("suite.file.file", fd.getFile());
}
catch (IOException e)
{
JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "Error reading "+historyFile, JOptionPane.ERROR_MESSAGE);
}
}
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 doLoad()
{
FileDialog fd = new FileDialog(getFrame(), "Load History File", FileDialog.LOAD);
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.loadHistory(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);
}
}
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);
}
}
/**
* Displays a standard AWT file dialog for choosing a file for loading or
* saving.
*
* @param frame
* parent frame
* @param title
* dialog title
* @param path
* base directory (or null)
* @param formats
* an array of allowed file extensions (or null to allow all)
* @param mode
* either FileUtils.LOAD or FileUtils.SAVE
* @return path to chosen file or null, if user has canceled
*/
public static String showFileDialog(final Frame frame, final String title,
String path, final String[] formats, final int mode) {
String fileID = null;
FileDialog fd = new FileDialog(frame, title, mode);
if (path != null) {
fd.setDirectory(path);
}
if (formats != null) {
fd.setFilenameFilter(new FilenameFilter() {
public boolean accept(File dir, String name) {
boolean isAccepted = false;
for (String ext : formats) {
if (name.indexOf(ext) != -1) {
isAccepted = true;
break;
}
}
return isAccepted;
}
});
}
fd.setVisible(true);
if (fd.getFile() != null) {
fileID = fd.getFile();
fileID = fd.getDirectory() + fileID;
}
return fileID;
}
/**
*
*
* @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;
}
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;
}
/**
* 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());
}
}
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);
}
}
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());
}
}
@Override
public void sendKeys(CharSequence... keysToSend) {
FileDialog fileDialog = (FileDialog) dialog.getWindow();
String filePath = (String) keysToSend[0];
String setPath = "";
if (filePath != null && !"".equals(filePath)) {
setPath = ChooserHelper.decodeFile(filePath).getPath();
}
fileDialog.setFile(setPath);
fileDialog.setVisible(false);
}
private File getLoadFile(String filename, String extension) {
FileDialog fd = new FileDialog((JDialog) getRootPane().getTopLevelAncestor(),
Localizer.localize("LoadScannerOptionsFromFileTitle"), FileDialog.LOAD);
fd.setDirectory(".");
FilenameExtensionFilter filter = new FilenameExtensionFilter();
filter.addExtension("xml");
fd.setFilenameFilter(filter);
fd.setModal(true);
fd.setVisible(true);
if (fd.getFile() == null) {
return null;
}
return new File(fd.getDirectory() + File.separator + fd.getFile());
}
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);
}
private void showFilePicker() {
SwingWorker worker = new SwingWorker() {
@Override
public Object construct() {
try {
Thread.sleep(10);
}
catch (InterruptedException e1) {
Log.error(e1);
}
return true;
}
@Override
public void finished() {
FileDialog fileChooser = SparkManager.getTransferManager().getFileChooser(SparkManager.getChatManager().getChatContainer().getChatFrame(), Res.getString("title.select.file.to.send"));
if (SparkManager.getTransferManager().getDefaultDirectory() != null)
{
fileChooser.setDirectory(SparkManager.getTransferManager().getDefaultDirectory().getAbsolutePath());
}
fileChooser.setVisible(true);
final File[] files = fileChooser.getFiles();
if ( files.length == 0) {
// no selection
return;
}
File file = files[0]; // Single-file selection is used. Using the first array item is safe.
if (file.exists()) {
SparkManager.getTransferManager().setDefaultDirectory(file.getParentFile());
SparkManager.getTransferManager().sendFile(file, ((ChatRoomImpl)chatRoom).getParticipantJID());
}
}
};
worker.start();
}
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);
}
}
}
/**
* 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());
}
}
@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;
}
/**
* 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);
}
}
public void actionPerformed (ActionEvent ae) {
FileDialog fd = new FileDialog (frame, "Image Selector", FileDialog.LOAD);
fd.setVisible(true);
}
/**
* 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);
}
}