java.awt.FileDialog#getFile ( )源码实例Demo

下面列出了java.awt.FileDialog#getFile ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: EchoSim   文件: ScriptPanel.java
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);
    }
}
 
源代码2 项目: pumpernickel   文件: ImageQuantizationDemo.java
/**
 * 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());
}
 
源代码3 项目: netbeans   文件: FileChooserBuilder.java
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;
}
 
源代码4 项目: mpcmaid   文件: MainFrame.java
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();
	}
}
 
源代码5 项目: EchoSim   文件: TestingPanel.java
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);
    }
}
 
源代码6 项目: RDMP1   文件: rtest3.java
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;
}
 
源代码7 项目: CQL   文件: FileChooser.java
/**
 *
 *
 * @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;
}
 
源代码8 项目: java_jail   文件: Picture.java
/**
 * 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());
    }
}
 
源代码9 项目: pumpernickel   文件: FileDialogUtils.java
/**
 * 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();
}
 
源代码10 项目: algs4   文件: StdDraw.java
/**
 * 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());
    }
}
 
源代码11 项目: java_jail   文件: Picture.java
/**
 * 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());
    }
}
 
源代码12 项目: jmg   文件: Notate.java
/**
 * 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());
    }
}
 
源代码13 项目: jmg   文件: Read.java
/**
* 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());                
    }
}
 
源代码14 项目: binnavi   文件: CFileChooser.java
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;
}
 
源代码15 项目: pumpernickel   文件: FilePanel.java
/**
 * 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);
	}
}
 
源代码16 项目: JWT4B   文件: JWTInterceptTabController.java
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);	
		}
	}
}
 
源代码17 项目: dragonwell8_jdk   文件: PolicyTool.java
/**
 * 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);
    }
}
 
源代码18 项目: openjdk-jdk8u-backup   文件: PolicyTool.java
/**
 * 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);
    }
}
 
源代码19 项目: jdk8u-jdk   文件: PolicyTool.java
/**
 * 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);
    }
}
 
源代码20 项目: jdk8u_jdk   文件: PolicyTool.java
/**
 * 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);
    }
}