• category: swing folder: DisableNewFolderAction title: JFileChooserで新規フォルダ作成を無効化する tags: [JFileChooser] author: aterai pubdate: 2022-05-09T03:24:27+09:00 description: JFileChooserで既存ファイルの名前変更は有効のまま新規フォルダの作成のみ無効に設定します。 image: https://drive.google.com/uc?id=1a7Lm2pVbgfsEnORKxYiekYuJz_rAwCCI

概要

JFileChooserで既存ファイルの名前変更は有効のまま新規フォルダの作成のみ無効に設定します。

サンプルコード

JFileChooser fc2 = new JFileChooser();
fc2.setFileSystemView(new FileSystemView() {
  @Override public File createNewFolder(File containingDir) {
    return null;
  }
});
String cmd = "New Folder"; // = sun.swing.FilePane.ACTION_NEW_FOLDER
Optional.ofNullable(fc2.getActionMap().get(cmd)).ifPresent(a -> a.setEnabled(false));
fc2.addPropertyChangeListener(JFileChooser.DIRECTORY_CHANGED_PROPERTY, e -> {
  if (e.getNewValue() instanceof File) {
    Optional.ofNullable(fc2.getActionMap().get(cmd)).ifPresent(a -> a.setEnabled(false));
  }
});
View in GitHub: Java, Kotlin

解説

参考リンク

コメント