下面列出了怎么用java.awt.FileDialog的API类实例代码及写法,或者点击链接到github查看源代码。
public void sendFileTo(ContactItem item) {
FileDialog fileChooser = getFileChooser(SparkManager.getMainWindow(), Res.getString("title.select.file.to.send"));
if (defaultDirectory != null)
{
fileChooser.setDirectory( defaultDirectory.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()) {
defaultDirectory = file.getParentFile();
sendFile( file, item.getJid() );
}
}
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 init() {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
Sysout.createDialogWithInstructions(new String[]{
"Press PASS, this test is for MacOS X only."});
return;
}
System.setProperty("apple.awt.use-file-dialog-packages", "true");
setLayout(new GridLayout(1, 1));
fd = new FileDialog(new Frame(), "Open");
fd.setDirectory(APPLICATIONS_FOLDER);
showBtn = new Button("Show File Dialog");
showBtn.addActionListener(this);
add(showBtn);
String[] instructions = {
"1) Click on 'Show File Dialog' button. A file dialog will come up.",
"2) Navigate to the Applications folder if not already there",
"3) Check that the application bundles can be selected and can not be navigated",
"4) If it's true then the test passed, otherwise it failed."};
Sysout.createDialogWithInstructions(instructions);
}
@Override
public void init() {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
Sysout.createDialogWithInstructions(new String[]{
"Press PASS, this test is for MacOS X only."});
return;
}
System.setProperty("apple.awt.fileDialogForDirectories", "true");
setLayout(new GridLayout(1, 1));
fd = new FileDialog(new Frame(), "Open");
showBtn = new Button("Show File Dialog");
showBtn.addActionListener(this);
add(showBtn);
String[] instructions = {
"1) Click on 'Show File Dialog' button. A file dialog will come up.",
"2) Check that files can't be selected.",
"3) Check that directories can be selected.",
"4) Repeat steps 1 - 3 a few times for different files and directories.",
"5) If it's true then the test passed, otherwise it failed."};
Sysout.createDialogWithInstructions(instructions);
}
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());
}
@Override
public void init() {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
Sysout.createDialogWithInstructions(new String[]{
"Press PASS, this test is for MacOS X only."});
return;
}
System.setProperty("apple.awt.use-file-dialog-packages", "true");
setLayout(new GridLayout(1, 1));
fd = new FileDialog(new Frame(), "Open");
fd.setDirectory(APPLICATIONS_FOLDER);
showBtn = new Button("Show File Dialog");
showBtn.addActionListener(this);
add(showBtn);
String[] instructions = {
"1) Click on 'Show File Dialog' button. A file dialog will come up.",
"2) Navigate to the Applications folder if not already there",
"3) Check that the application bundles can be selected and can not be navigated",
"4) If it's true then the test passed, otherwise it failed."};
Sysout.createDialogWithInstructions(instructions);
}
@Override
public void init() {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
Sysout.createDialogWithInstructions(new String[]{
"Press PASS, this test is for MacOS X only."});
return;
}
System.setProperty("apple.awt.fileDialogForDirectories", "true");
setLayout(new GridLayout(1, 1));
fd = new FileDialog(new Frame(), "Open");
showBtn = new Button("Show File Dialog");
showBtn.addActionListener(this);
add(showBtn);
String[] instructions = {
"1) Click on 'Show File Dialog' button. A file dialog will come up.",
"2) Check that files can't be selected.",
"3) Check that directories can be selected.",
"4) Repeat steps 1 - 3 a few times for different files and directories.",
"5) If it's true then the test passed, otherwise it failed."};
Sysout.createDialogWithInstructions(instructions);
}
/**
* @param temporary path to copy to new location
* @param message displayed message in save file dialog pop up
*/
private void copyFile(Path temporary, String message) {
loadDialog.setMode(FileDialog.SAVE);
loadDialog.setTitle(message);
loadDialog.setVisible(true);
File[] files = loadDialog.getFiles();
if (files.length > 0) {
File file = files[0];
Path path = Paths.get(file.toURI());
try {
Files.copy(temporary, path, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, e.getMessage());
}
}
}
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());
}
@Override
public void init() {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
Sysout.createDialogWithInstructions(new String[]{
"Press PASS, this test is for MacOS X only."});
return;
}
System.setProperty("apple.awt.use-file-dialog-packages", "true");
setLayout(new GridLayout(1, 1));
fd = new FileDialog(new Frame(), "Open");
fd.setDirectory(APPLICATIONS_FOLDER);
showBtn = new Button("Show File Dialog");
showBtn.addActionListener(this);
add(showBtn);
String[] instructions = {
"1) Click on 'Show File Dialog' button. A file dialog will come up.",
"2) Navigate to the Applications folder if not already there",
"3) Check that the application bundles can be selected and can not be navigated",
"4) If it's true then the test passed, otherwise it failed."};
Sysout.createDialogWithInstructions(instructions);
}
@Override
public void init() {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
Sysout.createDialogWithInstructions(new String[]{
"Press PASS, this test is for MacOS X only."});
return;
}
System.setProperty("apple.awt.fileDialogForDirectories", "true");
setLayout(new GridLayout(1, 1));
fd = new FileDialog(new Frame(), "Open");
showBtn = new Button("Show File Dialog");
showBtn.addActionListener(this);
add(showBtn);
String[] instructions = {
"1) Click on 'Show File Dialog' button. A file dialog will come up.",
"2) Check that files can't be selected.",
"3) Check that directories can be selected.",
"4) Repeat steps 1 - 3 a few times for different files and directories.",
"5) If it's true then the test passed, otherwise it failed."};
Sysout.createDialogWithInstructions(instructions);
}
/**
* 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 filter
* a FilenameFilter implementation (or null)
* @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, FilenameFilter filter, final int mode) {
String fileID = null;
FileDialog fd = new FileDialog(frame, title, mode);
if (path != null) {
fd.setDirectory(path);
}
if (filter != null) {
fd.setFilenameFilter(filter);
}
fd.setVisible(true);
if (fd.getFile() != null) {
fileID = fd.getFile();
fileID = fd.getDirectory() + fileID;
}
return fileID;
}
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());
}
protected void doUtteranceLoadFile()
{
FileDialog fd = new FileDialog(getFrame(), "Load Utterance File", FileDialog.LOAD);
fd.setDirectory(RuntimeLogic.getProp("utterance.file.dir"));
fd.setFile(RuntimeLogic.getProp("utterance.file.file"));
fd.setVisible(true);
if (fd.getDirectory() == null)
return;
String intentFile = fd.getDirectory()+System.getProperty("file.separator")+fd.getFile();
if ((intentFile == null) || (intentFile.length() == 0))
return;
try
{
RuntimeLogic.readUtterances(mRuntime, (new File(intentFile)).toURI());
RuntimeLogic.setProp("utterance.file.dir", fd.getDirectory());
RuntimeLogic.setProp("utterance.file.file", fd.getFile());
}
catch (IOException e)
{
JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "Error reading "+intentFile, JOptionPane.ERROR_MESSAGE);
}
}
/**
* Show an open dialog with a file chooser set up according to the
* parameters of this builder.
* @return A file if the user clicks the accept button and a file or
* folder was selected at the time the user clicked cancel.
*/
public File showOpenDialog() {
JFileChooser chooser = createFileChooser();
if( Boolean.getBoolean("nb.native.filechooser") ) { //NOI18N
FileDialog fileDialog = createFileDialog( chooser.getCurrentDirectory() );
if( null != fileDialog ) {
return showFileDialog(fileDialog, FileDialog.LOAD );
}
}
chooser.setMultiSelectionEnabled(false);
int dlgResult = chooser.showOpenDialog(findDialogParent());
if (JFileChooser.APPROVE_OPTION == dlgResult) {
File result = chooser.getSelectedFile();
if (result != null && !result.exists()) {
result = null;
}
return result;
} else {
return null;
}
}
/**
* Opens the file dialog and saves the selected Petri net into lastLoadedPetriNet
* for use when calculating the state space exploration
*/
private void loadData() {
loadDialog.setMode(FileDialog.LOAD);
loadDialog.setTitle("Select petri net");
loadDialog.setVisible(true);
File[] files = loadDialog.getFiles();
if (files.length > 0) {
File path = files[0];
try {
petriNetNameLabel.setText(path.getName());
PetriNetReader petriNetIO = new PetriNetIOImpl();
lastLoadedPetriNet = petriNetIO.read(path.getAbsolutePath());
} catch (JAXBException | FileNotFoundException e) {
LOGGER.log(Level.SEVERE, e.getMessage());
}
}
}
protected void doIntentLoadFile()
{
FileDialog fd = new FileDialog(getFrame(), "Load Intent File", FileDialog.LOAD);
fd.setDirectory(RuntimeLogic.getProp("intent.file.dir"));
fd.setFile(RuntimeLogic.getProp("intent.file.file"));
fd.setVisible(true);
if (fd.getDirectory() == null)
return;
String intentFile = fd.getDirectory()+System.getProperty("file.separator")+fd.getFile();
if ((intentFile == null) || (intentFile.length() == 0))
return;
try
{
RuntimeLogic.readIntents(mRuntime, (new File(intentFile)).toURI());
RuntimeLogic.setProp("intent.file.dir", fd.getDirectory());
RuntimeLogic.setProp("intent.file.file", fd.getFile());
}
catch (IOException e)
{
JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "Error reading "+intentFile, JOptionPane.ERROR_MESSAGE);
}
}
@Override
public void init() {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
Sysout.createDialogWithInstructions(new String[]{
"Press PASS, this test is for MacOS X only."});
return;
}
System.setProperty("apple.awt.use-file-dialog-packages", "true");
setLayout(new GridLayout(1, 1));
fd = new FileDialog(new Frame(), "Open");
fd.setDirectory(APPLICATIONS_FOLDER);
showBtn = new Button("Show File Dialog");
showBtn.addActionListener(this);
add(showBtn);
String[] instructions = {
"1) Click on 'Show File Dialog' button. A file dialog will come up.",
"2) Navigate to the Applications folder if not already there",
"3) Check that the application bundles can be selected and can not be navigated",
"4) If it's true then the test passed, otherwise it failed."};
Sysout.createDialogWithInstructions(instructions);
}
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);
}
}
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();
}
}
if (!quit) {
run(fd.getTitle(), fd.getMode(), dirname, filename,
fd.getFilenameFilter(), fd.isMultipleMode(), fd.getX(), fd.getY());
}
}
@Override
public void init() {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
Sysout.createDialogWithInstructions(new String[]{
"Press PASS, this test is for MacOS X only."});
return;
}
System.setProperty("apple.awt.use-file-dialog-packages", "true");
setLayout(new GridLayout(1, 1));
fd = new FileDialog(new Frame(), "Open");
fd.setDirectory(APPLICATIONS_FOLDER);
showBtn = new Button("Show File Dialog");
showBtn.addActionListener(this);
add(showBtn);
String[] instructions = {
"1) Click on 'Show File Dialog' button. A file dialog will come up.",
"2) Navigate to the Applications folder if not already there",
"3) Check that the application bundles can be selected and can not be navigated",
"4) If it's true then the test passed, otherwise it failed."};
Sysout.createDialogWithInstructions(instructions);
}
@Override
public void init() {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
Sysout.createDialogWithInstructions(new String[]{
"Press PASS, this test is for MacOS X only."});
return;
}
System.setProperty("apple.awt.fileDialogForDirectories", "true");
setLayout(new GridLayout(1, 1));
fd = new FileDialog(new Frame(), "Open");
showBtn = new Button("Show File Dialog");
showBtn.addActionListener(this);
add(showBtn);
String[] instructions = {
"1) Click on 'Show File Dialog' button. A file dialog will come up.",
"2) Check that files can't be selected.",
"3) Check that directories can be selected.",
"4) Repeat steps 1 - 3 a few times for different files and directories.",
"5) If it's true then the test passed, otherwise it failed."};
Sysout.createDialogWithInstructions(instructions);
}
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());
}
public void doBrowseForFile(AudioPlayerComponent apc) {
Window w = SwingUtilities.getWindowAncestor(apc);
if (!(w instanceof Frame))
throw new RuntimeException(
"cannot invoke a FileDialog if the player is not in a java.awt.Frame");
// the button shouldn't be enabled if w isn't a Frame...
Frame f = (Frame) w;
FileDialog fd = new FileDialog(f);
fd.pack();
fd.setLocationRelativeTo(null);
fd.setVisible(true);
if (fd.getFile() == null)
throw new UserCancelledException();
File file = new File(fd.getDirectory() + fd.getFile());
try {
apc.setSource(file.toURI().toURL());
} catch (MalformedURLException e) {
e.printStackTrace();
apc.setSource(null);
}
}
@Override
public void init() {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
Sysout.createDialogWithInstructions(new String[]{
"Press PASS, this test is for MacOS X only."});
return;
}
System.setProperty("apple.awt.use-file-dialog-packages", "true");
setLayout(new GridLayout(1, 1));
fd = new FileDialog(new Frame(), "Open");
fd.setDirectory(APPLICATIONS_FOLDER);
showBtn = new Button("Show File Dialog");
showBtn.addActionListener(this);
add(showBtn);
String[] instructions = {
"1) Click on 'Show File Dialog' button. A file dialog will come up.",
"2) Navigate to the Applications folder if not already there",
"3) Check that the application bundles can be selected and can not be navigated",
"4) If it's true then the test passed, otherwise it failed."};
Sysout.createDialogWithInstructions(instructions);
}
@Override
public void init() {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
Sysout.createDialogWithInstructions(new String[]{
"Press PASS, this test is for MacOS X only."});
return;
}
System.setProperty("apple.awt.fileDialogForDirectories", "true");
setLayout(new GridLayout(1, 1));
fd = new FileDialog(new Frame(), "Open");
showBtn = new Button("Show File Dialog");
showBtn.addActionListener(this);
add(showBtn);
String[] instructions = {
"1) Click on 'Show File Dialog' button. A file dialog will come up.",
"2) Check that files can't be selected.",
"3) Check that directories can be selected.",
"4) Repeat steps 1 - 3 a few times for different files and directories.",
"5) If it's true then the test passed, otherwise it failed."};
Sysout.createDialogWithInstructions(instructions);
}
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());
}
@Override
public void init() {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
Sysout.createDialogWithInstructions(new String[]{
"Press PASS, this test is for MacOS X only."});
return;
}
System.setProperty("apple.awt.use-file-dialog-packages", "true");
setLayout(new GridLayout(1, 1));
fd = new FileDialog(new Frame(), "Open");
fd.setDirectory(APPLICATIONS_FOLDER);
showBtn = new Button("Show File Dialog");
showBtn.addActionListener(this);
add(showBtn);
String[] instructions = {
"1) Click on 'Show File Dialog' button. A file dialog will come up.",
"2) Navigate to the Applications folder if not already there",
"3) Check that the application bundles can be selected and can not be navigated",
"4) If it's true then the test passed, otherwise it failed."};
Sysout.createDialogWithInstructions(instructions);
}
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());
}
@Override
public void init() {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
Sysout.createDialogWithInstructions(new String[]{
"Press PASS, this test is for MacOS X only."});
return;
}
System.setProperty("apple.awt.fileDialogForDirectories", "true");
setLayout(new GridLayout(1, 1));
fd = new FileDialog(new Frame(), "Open");
showBtn = new Button("Show File Dialog");
showBtn.addActionListener(this);
add(showBtn);
String[] instructions = {
"1) Click on 'Show File Dialog' button. A file dialog will come up.",
"2) Check that files can't be selected.",
"3) Check that directories can be selected.",
"4) Repeat steps 1 - 3 a few times for different files and directories.",
"5) If it's true then the test passed, otherwise it failed."};
Sysout.createDialogWithInstructions(instructions);
}