44

テキストファイルからデータをロードおよび保存するプログラムに取り組んでおり、ロードおよび保存時に JFileChooser を使用してユーザーにファイル名を尋ねています。

この質問は、保存ダイアログに関するものです: new JFileChooser().showSaveDialog();. その後、ユーザーは警告なしに既存のファイルを上書きする可能性があり、それは問題になります

これを修正する方法について何か提案はありますか? 方法やオプションを探していましたが、何も見つかりませんでした。

前もって感謝します。

4

5 に答える 5

88

答えてくれてありがとう、しかし私は別の回避策を見つけました。

JFileChooser example = new JFileChooser(){
    @Override
    public void approveSelection(){
        File f = getSelectedFile();
        if(f.exists() && getDialogType() == SAVE_DIALOG){
            int result = JOptionPane.showConfirmDialog(this,"The file exists, overwrite?","Existing file",JOptionPane.YES_NO_CANCEL_OPTION);
            switch(result){
                case JOptionPane.YES_OPTION:
                    super.approveSelection();
                    return;
                case JOptionPane.NO_OPTION:
                    return;
                case JOptionPane.CLOSED_OPTION:
                    return;
                case JOptionPane.CANCEL_OPTION:
                    cancelSelection();
                    return;
            }
        }
        super.approveSelection();
    }        
}

これが他の誰かに役立つことを願っています。

于 2010-09-16T17:30:29.813 に答える
6

AvrDragon が言ったように、X で閉じることは処理されません。無関係なオプションをすべて処理するために、デフォルトのケースを追加しました。

final JFileChooser fc = new JFileChooser() {

        private static final long serialVersionUID = 7919427933588163126L;

        public void approveSelection() {
            File f = getSelectedFile();
            if (f.exists() && getDialogType() == SAVE_DIALOG) {
                int result = JOptionPane.showConfirmDialog(this,
                        "The file exists, overwrite?", "Existing file",
                        JOptionPane.YES_NO_CANCEL_OPTION);
                switch (result) {
                case JOptionPane.YES_OPTION:
                    super.approveSelection();
                    return;
                case JOptionPane.CANCEL_OPTION:
                    cancelSelection();
                    return;
                default:
                    return;
                }
            }
            super.approveSelection();
        }
    };
于 2012-03-15T08:50:03.467 に答える
3

保存する前に、同じファイルが既に存在するかどうかを確認してから、ユーザーに確認を求めて、本当にオーバーライドしたいですか :p

 JDialog.setDefaultLookAndFeelDecorated(true);
    int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to override existing file?", "Confirm",
        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    if (response == JOptionPane.NO_OPTION) {
      System.out.println("No button clicked");
    } else if (response == JOptionPane.YES_OPTION) {
      System.out.println("Yes button clicked");
    } else if (response == JOptionPane.CLOSED_OPTION) {
      System.out.println("JOptionPane closed");
    }  

ここにコードがあります

ファイルが既に存在することを確認するには

boolean exists = (new File("filename")).exists();
if (exists) {
    // File or directory exists
} else {
    // File or directory does not exist
}
于 2010-09-06T12:45:53.453 に答える
1

たぶん、ファイルがまだ存在していないことを確認し、JFileChooserにFileSystemViewこのコンストラクターを参照)を与えることもできます。

于 2010-09-06T12:52:19.057 に答える
1

私はあなた自身の答えに基づいてこれを書きました。他の誰かが役に立つと思った場合に投稿:

final JFileChooser exportFileChooser = new JFileChooser();
exportFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
exportFileChooser.setApproveButtonText("Export");

final JButton exportButton = new JButton("Export text file");
exportButton.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        int returnVal = exportFileChooser.showSaveDialog(exportButton
                .getParent());

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File outputFile = exportFileChooser.getSelectedFile();
            if (outputFileIsValid(outputFile)) {
                exportFile(outputFile);
            }
        }
    }

    private boolean outputFileIsValid(File outputFile) {
        boolean fileIsValid = false;
        if (outputFile.exists()) {
            int result = JOptionPane.showConfirmDialog(
                    exportButton.getParent(),
                    "File exists, overwrite?", "File exists",
                    JOptionPane.YES_NO_CANCEL_OPTION);
            switch (result) {
            case JOptionPane.YES_OPTION:
                fileIsValid = true;
                break;
            default:
                fileIsValid = false;
            }
        } else {
            fileIsValid = true;
        }
        return fileIsValid;
    }
});
于 2012-01-24T17:23:22.057 に答える