java.awt.FileDialog类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(11.4k)|赞(0)|评价(0)|浏览(198)

本文整理了Java中java.awt.FileDialog类的一些代码示例,展示了FileDialog类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileDialog类的具体详情如下:
包路径:java.awt.FileDialog
类名称:FileDialog

FileDialog介绍

暂无

代码示例

代码示例来源:origin: libgdx/libgdx

File getDirectory () {
  if (System.getProperty("os.name").contains("Mac")) {
    System.setProperty("apple.awt.fileDialogForDirectories", "true");
    FileDialog dialog = new FileDialog(GdxSetupUI.this, "Choose destination", FileDialog.LOAD);
    dialog.setVisible(true);
    String name = dialog.getFile();
    String dir = dialog.getDirectory();
    if (name == null || dir == null) return null;
    return new File(dialog.getDirectory(), dialog.getFile());
  } else {
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setDialogTitle("Choose destination");
    int result = chooser.showOpenDialog(null);
    if (result == JFileChooser.APPROVE_OPTION) {
      File dir = chooser.getSelectedFile();
      if (dir == null) return null;
      if (dir.getAbsolutePath().trim().length() == 0) return null;
      return dir;
    } else {
      return null;
    }
  }
}

代码示例来源:origin: libgdx/libgdx

private File showFileDialog (String title, int mode ) {
  FileDialog dialog = new FileDialog(this, title, mode);
  if (lastDir != null) dialog.setDirectory(lastDir);
  dialog.setVisible(true);
  final String file = dialog.getFile();
  final String dir = dialog.getDirectory();
  if (dir == null || file == null || file.trim().length() == 0) 
    return null;
  lastDir = dir;
  return new File(dir, file);
}

代码示例来源:origin: stackoverflow.com

FileDialog fd = new FileDialog(yourJFrame, "Choose a file", FileDialog.LOAD);
fd.setDirectory("C:\\");
fd.setFile("*.xml");
fd.setVisible(true);
String filename = fd.getFile();
if (filename == null)
 System.out.println("You cancelled the choice");
else
 System.out.println("You chose " + filename);

代码示例来源:origin: mirador/mirador

FileDialog dialog = new FileDialog(parentFrame, prompt, mode);
if (defaultSelection != null) {
 dialog.setDirectory(defaultSelection.getParent());
 dialog.setFile(defaultSelection.getName());
dialog.setFilenameFilter(new FilenameFilter() {
 public boolean accept(File dir, String name) {
  if (name.startsWith(".")) return false;
dialog.setVisible(true);
String directory = dialog.getDirectory();
String filename = dialog.getFile();
if (filename != null) {
 selectedFile = new File(directory, filename);
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(prompt);
if (defaultSelection != null) {
 chooser.setSelectedFile(defaultSelection);

代码示例来源:origin: edu.toronto.cs.savant/savant-core

FileDialog fd = getFileDialog(title, FileDialog.LOAD);
if (filter != null) {
  fd.setFilenameFilter(new FilenameFilterAdapter(filter));
  fd.setDirectory(initialDir.getAbsolutePath());
fd.setVisible(true);
fd.setAlwaysOnTop(true);
String selectedFileName = fd.getFile();
if (selectedFileName != null) {
  return new File(fd.getDirectory(), selectedFileName);
JFileChooser fd = new JFileChooser();
fd.setDialogTitle(title);
fd.setDialogType(JFileChooser.OPEN_DIALOG);
if (filter != null) {
  fd.setFileFilter(filter);

代码示例来源:origin: com.nativelibs4java/javacl-demos

static File chooseFile() {
  if (Platform.isMacOSX()) {
    FileDialog d = new FileDialog((java.awt.Frame)null);
    d.setMode(FileDialog.LOAD);
    d.show();
    String f = d.getFile();
    if (f != null)
      return new File(new File(d.getDirectory()), d.getFile());
  } else {
    JFileChooser chooser = new JFileChooser();
    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
      return chooser.getSelectedFile();
  }
  return null;
}
 public static void main(String[] args) {

代码示例来源:origin: sc.fiji/bUnwarpJ_

if( Prefs.useFileChooser )
  JFileChooser chooser = new JFileChooser();
  chooser.setDialogTitle( dialogTitle );
  chooser.setCurrentDirectory( new File( currentFolder ));
  chooser.setVisible( true );
  chooser.setDialogType( saveDialog ? JFileChooser.SAVE_DIALOG :
  FileDialog fd = new FileDialog( IJ.getInstance(),
      dialogTitle, saveDialog ? FileDialog.SAVE : FileDialog.LOAD );
  fd.setDirectory( currentFolder );
  fd.setVisible( true );
  path = fd.getDirectory() + File.separator;
  filename = fd.getName();

代码示例来源:origin: nativelibs4java/JavaCL

public static File chooseFile(File initialFile, boolean load) {
  if (isMac()) {
    FileDialog d = new FileDialog((java.awt.Frame)null);
    d.setMode(load ? FileDialog.LOAD : FileDialog.SAVE);
    if (initialFile != null) {
      d.setDirectory(initialFile.getParent());
      d.setFile(initialFile.getName());
    }
    d.show();
    String f = d.getFile();
    if (f != null)
      return new File(new File(d.getDirectory()), d.getFile());
  } else {
    JFileChooser chooser = new JFileChooser();
    if (initialFile != null)
      chooser.setSelectedFile(initialFile);
    if ((load ? chooser.showOpenDialog(null) : chooser.showSaveDialog(null)) == JFileChooser.APPROVE_OPTION)
        return chooser.getSelectedFile();
  }
  return null;
}

代码示例来源:origin: AliView/AliView

FileDialog dialog = new FileDialog(parentFrame, "Save",FileDialog.SAVE);
dialog.setDirectory(suggestedFile.getParent());
dialog.setFile(suggestedFile.getName());
dialog.setVisible(true);
String fileName = dialog.getFile();
String directory = dialog.getDirectory(); 
if(fileName != null && directory != null){
  selectedFile = new File(directory, fileName);
JFileChooser fc = new JFileChooser();
fc.setSelectedFile(suggestedFile);
fc.setPreferredSize(new Dimension(preferedWidth, preferedHeight));

代码示例来源:origin: AliView/AliView

FileDialog dialog = new FileDialog(parentFrame, "Open",FileDialog.LOAD);
dialog.setDirectory(suggestedFile.getParent());
dialog.setVisible(true);
String fileDirectory = dialog.getDirectory();
String fileName = dialog.getFile();
if(fileName != null){
  selectedFile = new File(fileDirectory, fileName);
UIManager.put("FileChooser.readOnly", Boolean.TRUE);  
JFileChooser fc = new JFileChooser(suggestedFile);
fc.setPreferredSize(new Dimension(preferedWidth, preferedHeight));
int returnVal = fc.showOpenDialog(parentComponent);

代码示例来源:origin: sc.fiji/TrackMate_

final FileDialog dialog =	new FileDialog(parent, title, FileDialog.LOAD);
  dialog.setIconImage(TRACKMATE_ICON.getImage());
  dialog.setDirectory(file.getParent());
  dialog.setFile(file.getName());
  final FilenameFilter filter = new FilenameFilter() {
    @Override
  dialog.setFilenameFilter(filter);
  dialog.setVisible(true);
  String selectedFile = dialog.getFile();
  if (null == selectedFile) {
    logger.log("Load data aborted.\n");
  file = new File(dialog.getDirectory(), selectedFile);
} else {
  final JFileChooser fileChooser = new JFileChooser(file.getParent()) {
  fileChooser.setName(title);
  fileChooser.setSelectedFile(file);
  final FileNameExtensionFilter filter = new FileNameExtensionFilter("XML files", "xml");
  fileChooser.setFileFilter(filter);
  final int returnVal = fileChooser.showOpenDialog(parent);

代码示例来源:origin: jawi/ols

if ( aOwner instanceof Dialog )
 dialog = new FileDialog( ( Dialog )aOwner );
 dialog = new FileDialog( ( Frame )aOwner );
dialog.setDirectory( aCurrentDirectory );
 dialog.setFilenameFilter( new FilenameFilterAdapter( aFileFilters ) );
 dialog.setVisible( true );
 final String selectedFile = dialog.getFile();
 return ( selectedFile == null ) ? null : new File( dialog.getDirectory(), selectedFile );
 dialog.dispose();
final JFileChooser dialog = new JFileChooser();
dialog.setCurrentDirectory( ( aCurrentDirectory == null ) ? null : new File( aCurrentDirectory ) );
 dialog.addChoosableFileFilter( filter );

代码示例来源:origin: triplea-game/triplea

final FileDialog fileDialog = new FileDialog(frame);
 fileDialog.setMode(FileDialog.SAVE);
 fileDialog.setDirectory(ClientSetting.saveGamesFolderPath.getValueOrThrow().toString());
 fileDialog.setFilenameFilter((dir, name) -> GameDataFileUtils.isCandidateFileName(name));
 fileDialog.setVisible(true);
 final String fileName = fileDialog.getFile();
 if (fileName == null) {
  return null;
 return new File(fileDialog.getDirectory(), GameDataFileUtils.addExtensionIfAbsent(fileName));
final int selectedOption = fileChooser.showSaveDialog(frame);
if (selectedOption != JFileChooser.APPROVE_OPTION) {
 return null;
File f = fileChooser.getSelectedFile();
 if (!fileChooser.getCurrentDirectory().toString().equals(filePath)) {
  JOptionPane.showConfirmDialog(frame, "Sub directories are not allowed in the file name.  Please rename it.",
    "Cancel?", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);

代码示例来源:origin: stackoverflow.com

import java.awt.*;
import javax.swing.*;

class FileDialogs {

  public static void main(String[] args) {
    Runnable r = new Runnable() {

      @Override
      public void run() {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception useDefault) {
        }
        FileDialog fd = new FileDialog((Frame)null);
        fd.setVisible(true);

        JFileChooser fc = new JFileChooser();
        fc.showSaveDialog(null);
      }
    };
    // Swing GUIs should be created and updated on the EDT
    // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
    SwingUtilities.invokeLater(r);
  }
}

代码示例来源:origin: elki-project/elki

/**
 * Button callback to show the file selector
 */
@Override
public void actionPerformed(ActionEvent e) {
 FileDialog fc = new FileDialog(frame);
 fc.setDirectory(defaultpath);
 fc.setMode(mode);
 final String curr = textfield.getText();
 if(curr != null && curr.length() > 0) {
  fc.setFile(curr);
 }
 fc.setVisible(true);
 String filename = fc.getFile();
 if(filename != null) {
  textfield.setText(new File(fc.getDirectory(), filename).getPath());
 }
 fc.setVisible(false);
 fc.dispose();
 textfield.requestFocus();
 fireEditingStopped();
}

代码示例来源:origin: net.java.openjdk.cacio/cacio-shared

@Override
void postInitSwingComponent() {
  super.postInitSwingComponent();
  fileChooser = new JFileChooser();
  getSwingComponent().getContentPane().add(fileChooser);
  getSwingComponent().layout();
  FileDialog fd = (FileDialog) getAWTComponent();
  setFile(fd.getFile());
  setDirectory(fd.getDirectory());
  setFilenameFilter(fd.getFilenameFilter());
}

代码示例来源:origin: com.bbossgroups/bboss-htmlparser

/**
 * The action to take when "Open" menu or button pressed.
 */
protected void openAction ()
{
  FileDialog dialog;
  File file;
  dialog = new FileDialog (this);
  dialog.setMode (FileDialog.LOAD);
  dialog.setTitle ("Open");
  dialog.setDirectory (mHomeDir);
  dialog.setVisible (true);
  if (null != dialog.getFile ())
  {
    mHomeDir = dialog.getDirectory ();
    file = new File (mHomeDir + dialog.getFile ());
    open (file.getAbsolutePath ());
    setTitle (TITLE + " - " + file.getAbsolutePath ());
  }
}

代码示例来源:origin: AliView/AliView

private void testFileDialog(){
  Frame f = new Frame();
  FileDialog fd = new FileDialog(f, "Hej", FileDialog.LOAD);
  fd.setDirectory("/home/anders/_ormbunkar");
  fd.setLocation(100,100);
  fd.setSize(600, 600);
  fd.setFile("");
  fd.setVisible(true);
  File selectedFile = new File(fd.getFile());
}

代码示例来源:origin: triplea-game/triplea

/**
  * Opens up a UI pop-up allowing user to select a game file. Returns nothing if user closes the pop-up.
  */
 public static Optional<File> selectGameFile() {
  // For some strange reason, the only way to get a Mac OS X native-style file dialog
  // is to use an AWT FileDialog instead of a Swing JDialog
  if (SystemProperties.isMac()) {
   final FileDialog fileDialog = GameRunner.newFileDialog();
   fileDialog.setMode(FileDialog.LOAD);
   fileDialog.setDirectory(ClientSetting.saveGamesFolderPath.getValueOrThrow().toString());
   fileDialog.setFilenameFilter((dir, name) -> GameDataFileUtils.isCandidateFileName(name));
   fileDialog.setVisible(true);
   final String fileName = fileDialog.getFile();
   final String dirName = fileDialog.getDirectory();
   return Optional.ofNullable(fileName)
     .map(name -> new File(dirName, fileName));
  }

  return GameRunner.showSaveGameFileChooser();
 }
}

代码示例来源:origin: org.scijava/scijava-ui-awt

@Override
public void actionPerformed(final ActionEvent e) {
  File file = new File(path.getText());
  if (!file.isDirectory()) {
    file = file.getParentFile();
  }
  // display file chooser in appropriate mode
  final String style = get().getItem().getWidgetStyle();
  final FileDialog fileDialog = new FileDialog((Frame) null);
  if (FileWidget.SAVE_STYLE.equals(style)) {
    fileDialog.setMode(FileDialog.SAVE);
  }
  else { // default behavior
    fileDialog.setMode(FileDialog.LOAD);
  }
  fileDialog.setDirectory(file.getAbsolutePath());
  fileDialog.setVisible(true);
  final String filename = fileDialog.getFile();
  fileDialog.dispose();
  if (filename == null) return;
  path.setText(filename);
}

相关文章