怎样得到JFileChooser目录路径和文件名

解决方案 »

  1.   

    E:\编程\java se 6 documetion\docs\index.html
    哈哈……去看下documetion哦~
      

  2.   

    javax.swing.JFileChooser.getCurrentDirectory() 得到路径
    javax.swing.JFileChooser.getSelectedFile() 得到文件javax.swing.JFileChooser.getSelectedFiles() //Returns a list of selected files if the file chooser is set to allow multiple selection
      

  3.   

    直接可运行,JDK版本:1.4.2-b28 package demo;import java.awt.Dimension;
    import java.io.File;import javax.swing.JDialog;
    import javax.swing.JFileChooser;public class JFileChooserDemo{
        public JFileChooserDemo(){}
        
        public static void main(String[] args) {
            new JFileChooserDemo().getSelectPath();
        }
        
        public String getSelectPath(){
            String path=null;        
            JFileChooser openLicenseFile = new JFileChooser();
            openLicenseFile.setDialogTitle("文件选择");
            openLicenseFile.setApproveButtonText("选择");
            openLicenseFile.setApproveButtonToolTipText("文件选择");
            openLicenseFile.setSelectedFile(new File("*.doc"));
            openLicenseFile.setPreferredSize(new Dimension(600, 480));
            String dirName = openLicenseFile.getCurrentDirectory().toString().trim();
            if (dirName == null || dirName.trim().length() == 0) {
                openLicenseFile.setCurrentDirectory(new File("."));
            }else{
                openLicenseFile.setCurrentDirectory(new File(dirName));
            }
            int rtVal = openLicenseFile.showOpenDialog(new JDialog());
            if (rtVal == JFileChooser.APPROVE_OPTION) {
                path = openLicenseFile.getSelectedFile().getName();
            }
            return path;
        }
    }