a881127b
a881127b
2010-07-07 10:18

在批量转换文件文件之前设置转换后文件存取目录目录

已采纳

我写了个程序批量读取pdf文件到word中,现在的问题是在批量转换之前我要用户指定转换后文件的存放目录,这个用JChooser可以实现吗,我用如下代码:
[code="java"]fileDirectory=new JFileChooser();
int returnVal = fileDirectory.showSaveDialog(this);
System.out.println("returnVal:"+returnVal);
if(returnVal == fileDirectory.APPROVE_OPTION) {
saveDirectory=fileDirectory.getCurrentDirectory().toString();
System.out.println("choose the dictory is :"+saveDirectory);
}[/code]
但是对话框出来后,若没有选择文件,只是选择目录点击保存没有任何响应。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • XanPeng xanpeng 11年前

    你需要设置 SelectionMode: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    参见示例代码

    [code="java"]
    package com.xan.fortest;

    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;

    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class SwingTester extends JPanel implements ActionListener {
    private static final long serialVersionUID = 4537424836172729931L;

    JButton go;
    
    JFileChooser chooser;
    private final static String chooserTitle = "Folder Chooser";
    
    public SwingTester() {
        go = new JButton("Choose Folder");
        go.addActionListener(this);
        add(go);
    }
    
    public void actionPerformed(ActionEvent e) {
        chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle(chooserTitle);
    
        /******  SHOULD SET THIS **********/
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        //
        // disable the "All files" option.
        //
        chooser.setAcceptAllFileFilterUsed(false);
        //    
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            System.out.println("getCurrentDirectory(): "
                    + chooser.getCurrentDirectory());
            System.out.println("getSelectedFile() : "
                    + chooser.getSelectedFile());
        } else {
            System.out.println("No Selection ");
        }
    }
    
    public Dimension getPreferredSize() {
        return new Dimension(400, 400);
    }
    
    public static void main(String s[]) {
        JFrame frame = new JFrame("");
        SwingTester panel = new SwingTester();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.getContentPane().add(panel, "Center");
        frame.setSize(panel.getPreferredSize());
        frame.setVisible(true);
    }
    

    }
    [/code]

    点赞 评论 复制链接分享

相关推荐