JFileChooserを編集不可にする

編集者:Terai Atsuhiro~

作成日:2005-05-16
更新日:2022-05-09 (月) 03:27:09
  • 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

概要

JFileChooser内でのファイル名変更や新規フォルダ作成などの編集を不可にします。

概要

JFileChooserでのファイル名の変更や新規フォルダの作成を不可にします。

サンプルコード

UIManager.put("FileChooser.readOnly", Boolean.TRUE);

サンプルコード

#spanend
#spanadd
JButton readOnlyButton = new JButton("readOnly");
#spanend
#spanadd
readOnlyButton.addActionListener(e -> {
#spanend
  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());
  }
#spanadd
});
#spanend
#spanadd
View in GitHub: Java, Kotlin

解説

JDK 1.5.0以上でUIManager.put("FileChooser.readOnly", Boolean.TRUE);を設定すると、JFileChooserがリードオンリーになり、ファイル名の変更や新規フォルダの作成などが禁止されます。

解説

Java 1.5.0 以上の場合、UIManager.put("FileChooser.readOnly", Boolean.TRUE)とすることで、簡単にJFileChooserでのファイル名の変更や新規フォルダの作成を禁止することができます。

参考リンク

参考リンク

コメント

コメント