---
category: swing
folder: FileAlreadyExistsDialog
title: JFileChooserを開いたままファイルの上書き警告ダイアログを表示する
tags: [JFileChooser, JOptionPane]
author: aterai
pubdate: 2012-07-02T17:20:50+09:00
description: JFileChooserで名前を付けて保存する場合、すでに存在するファイルを選択してセーブボタンを押すと上書き警告ダイアログを表示するように設定します。
image: https://lh6.googleusercontent.com/-77H8Wl7UgV0/T_E-hZznv2I/AAAAAAAABOo/RIVqWmchCfw/s800/FileAlreadyExistsDialog.png
hreflang:
    href: http://java-swing-tips.blogspot.com/2012/08/jfilechooser-with-file-already-exists.html
    lang: en
---
* 概要 [#summary]
`JFileChooser`で名前を付けて保存する場合、すでに存在するファイルを選択してセーブボタンを押すと上書き警告ダイアログを表示するように設定します。

#download(https://lh6.googleusercontent.com/-77H8Wl7UgV0/T_E-hZznv2I/AAAAAAAABOo/RIVqWmchCfw/s800/FileAlreadyExistsDialog.png)

* サンプルコード [#sourcecode]
#code(link){{
JFileChooser fileChooser = new JFileChooser() {
  @Override public void approveSelection() {
    File f = getSelectedFile();
    if (f.exists() && getDialogType() == SAVE_DIALOG) {
      String m = String.format(
          "<html>%s already exists.<br>Do you want to replace it?",
          f.getAbsolutePath());
      int rv = JOptionPane.showConfirmDialog(
          this, m, "Save As", JOptionPane.YES_NO_OPTION);
      if (rv != JOptionPane.YES_OPTION) {
        return;
      }
    }
    super.approveSelection();
  }
};
}}

* 解説 [#explanation]
上記のサンプルでは、ユーザーが`Save`ボタンをクリックした時に呼び出される`JFileChooser#approveSelection()`をオーバーライドし、選択されているファイルがすでに存在するかどうかをチェックしています。存在しない場合は通常の`JFileChooser`の処理を実行し、存在する場合は上書きしても良いかを確認する`ConfirmDialog`を`JFileChooser`を親にして呼び出します。

- `ConfirmDialog`で`Yes`をクリック
-- `super.approveSelection()`を実行して結果を返して`JFileChooser`を閉じる
-- 上記のサンプルでは、このボタンをクリックしても選択しているファイル名をコンソールに出力するだけで、実際にそのファイルを保存したり、上書きする機能は実装していない
- `ConfirmDialog`で`No`をクリック
-- なにもせずに`JFileChooser`に戻る

* 参考リンク [#reference]
- [https://community.oracle.com/thread/1391852 Swing - How to react on events fired by a JFileChooser?]
- [http://stackoverflow.com/questions/3651494/jfilechooser-with-confirmation-dialog java - JFileChooser with confirmation dialog - Stack Overflow]

* コメント [#comment]
#comment
#comment