Summary

JTreeを使用して作成したフォルダ構造のTreeNodeWindowslnkショートカットの場合、JFileChooserShellFolderを利用してリンク先フォルダに移動可能にします。

Source Code Examples

private File getRealFile8(File file) {
  try {
    ShellFolder sf = ShellFolder.getShellFolder(file);
    if (sf.isLink()) {
      file = sf.getLinkLocation();
    }
  } catch (FileNotFoundException ex) {
    file = null;
  }
  return file;
}

// private File getRealFile9(File file) {
//   if (fileSystemView.isLink(file)) {
//     try {
//       file = fileSystemView.getLinkLocation(file);
//     } catch (FileNotFoundException ex) {
//       file = null;
//     }
//   }
//   return file;
// }

// private File getRealFile(File file) {
//   String version = System.getProperty("java.specification.version");
//   if (Double.parseDouble(version) >= 9.0) {
//     file = getRealFile9(file);
//   } else {
//     file = getRealFile8(file);
//   }
//   return file;
// }
View in GitHub: Java, Kotlin

Explanation

上記のサンプルでは、FileSystemViewを使ってディレクトリ構造をJTreeに表示するのようにJTreeを使用してディレクトリ構造を表示した場合でもJFileChooserと同様にWindows環境で作成されたlnkショートカットのリンク先を参照可能になるよう設定しています。

Reference

Comment