• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JFileChooserを編集不可にする
#navi(../)
#tags()
RIGHT:Posted by &author(aterai); at 2005-05-16
*JFileChooserを編集不可にする [#v2eca394]
JFileChooser内でのファイル名変更や新規フォルダ作成などの編集を不可にします。
---
category: swing
folder: ROFileChooser
title: JFileChooserを編集不可にする
tags: [JFileChooser, UIManager]
author: aterai
pubdate: 2005-05-16T06:02:26+09:00
description: JFileChooser内でのファイル名変更や新規フォルダ作成などの編集を不可にします。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTR_zuU1UI/AAAAAAAAAiE/nZgj97xKO24/s800/ROFileChooser.png
---
* 概要 [#summary]
`JFileChooser`内でのファイル名変更や新規フォルダ作成などの編集を不可にします。

//-&jnlp;
-&jar;
-&zip;
#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTR_zuU1UI/AAAAAAAAAiE/nZgj97xKO24/s800/ROFileChooser.png)

//#screenshot
#ref(http://lh4.ggpht.com/_9Z4BYR88imo/TQTR_zuU1UI/AAAAAAAAAiE/nZgj97xKO24/s800/ROFileChooser.png)

**サンプルコード [#u34dd39b]
* サンプルコード [#sourcecode]
#code(link){{
UIManager.put("FileChooser.readOnly", Boolean.TRUE);
JButton readOnlyButton = new JButton("readOnly");
readOnlyButton.addActionListener(e -> {
  UIManager.put("FileChooser.readOnly", Boolean.TRUE);
  JFileChooser fileChooser = new JFileChooser();
  int retValue = fileChooser.showOpenDialog(getRootPane());
  if (retValue == JFileChooser.APPROVE_OPTION) {
    log.setText(fileChooser.getSelectedFile().getAbsolutePath());
  }
});
}}

**解説 [#oa8f776b]
Java 1.5.0 以上の場合、UIManager.put("FileChooser.readOnly", Boolean.TRUE)とすることで、簡単にJFileChooserでのファイル名の変更や新規フォルダの作成を禁止することができます。
* 解説 [#explanation]
`JDK 1.5.0`以上で`UIManager.put("FileChooser.readOnly", Boolean.TRUE);`を設定すると、`JFileChooser`がリードオンリーになり、ファイル名の変更や新規フォルダの作成などが禁止されます。

**参考リンク [#t8d0c593]
-[http://forums.sun.com/thread.jspa?threadID=248479 Swing - disabling "rename" on JFileChooser]
* 参考リンク [#reference]
- [https://community.oracle.com/thread/1377535 Swing - disabling "rename" on JFileChooser]
- [[JFileChooserで読み取り専用ファイルのリネームを禁止>Swing/RenameIfCanWriteFileChooser]]
- [https://bugs.openjdk.org/browse/JDK-8021379 [JDK-8021379] JFileChooser Create New Folder button enabled in write proteced directory - Java Bug System]
-- `Java 8`で修正済み
- [[JFileChooserで新規フォルダ作成を無効化する>Swing/DisableNewFolderAction]]

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