Swing/FileChooserCurrentDirectory のバックアップの現在との差分(No.9)
- category: swing folder: FileChooserCurrentDirectory title: JFileChooserを開いた時のカレントディレクトリを設定する tags: [JFileChooser] author: aterai pubdate: 2012-09-17T13:37:55+09:00 description: JFileChooserを開いた時のカレントディレクトリを設定します。 image:
概要
概要
JFileChooser
を開いた時のカレントディレクトリを設定します。
Screenshot
Advertisement
サンプルコード
サンプルコード
File f = new File(field.getText().trim());
JFileChooser fc = check1.isSelected() ? fc2 : fc0;
fc.setCurrentDirectory(f);
int retvalue = fc.showOpenDialog(p);
if (retvalue == JFileChooser.APPROVE_OPTION) {
log.setText(fc.getSelectedFile().getAbsolutePath());
}
View in GitHub: Java, Kotlin解説
解説
JFileChooser.DIRECTORIES_ONLY
で、ディレクトリのみ表示する場合のカレントディレクトリの設定をテストします。
- setCurrentDirectory:
JFileChooser#setCurrentDirectory(File)
でCurrentDirectory
を設定 -
setCurrentDirectory
- JFileChooser#setCurrentDirectory(File)メソッドで
CurrentDirectory
を設定 - 参照: コンボボックスにディレクトリ名
- リストには、
CurrentDirectory
内のディレクトリ一覧 - リストには
CurrentDirectory
内のディレクトリ一覧 - フォルダ名: テキストフィールドは前回の文字列(
setCurrentDirectory
では変化しない) - 存在しないファイルを
setCurrentDirectory
で設定すると、前回のCurrentDirectory
(初回に存在しないファイルが設定された場合はOS
のデフォルト)が表示される- 上記のサンプルで、
Change !dir.exists() case
にチェックをした場合、前回のディレクトリではなく、参照可能な親ディレクトリを検索するよう、setCurrentDirectory
をオーバーライドしたJFileChooser
を使用する - 上記のサンプルで
Change !dir.exists() case
にチェックをした場合、前回のディレクトリではなく参照可能な親ディレクトリを検索するようsetCurrentDirectory
をオーバーライドしたJFileChooser
を使用するJFileChooser fc2 = new JFileChooser() { @Override public void setCurrentDirectory(File dir) { if (dir != null && !dir.exists()) { this.setCurrentDirectory(dir.getParentFile()); } super.setCurrentDirectory(dir); } };
- 上記のサンプルで、
- JFileChooser#setCurrentDirectory(File)メソッドで
- setSelectedFile:
JFileChooser#setSelectedFile(File)
で選択ファイルとしてディレクトリを設定- 参照: コンボボックスには、選択ファイルとして設定したディレクトリの親ディレクトリ名
- リストには、親ディレクトリ内のディレクトリ一覧
-
setSelectedFile
- JFileChooser#setSelectedFile(File)メソッドで選択ファイルとしてディレクトリを設定
- 参照: コンボボックスには選択ファイルとして設定したディレクトリの親ディレクトリ名
- リストには親ディレクトリ内のディレクトリ一覧
Metal
やNimbus LookAndFeel
では、選択ファイルとして設定したディレクトリが選択状態になる-
Metal
などのLookAndFeel
でも、ディレクトリが選択状態にならない場合がある - 上記のサンプルで、
isParent reset?
にチェックをした場合、!fileChooser.getFileSystemView().isParent(fileChooser.getCurrentDirectory(), dir)==false
になるように?setSelectedFile
で選択ファイルをリセットする -
Metal
などのLookAndFeel
でもディレクトリが選択状態にならない場合がある - 上記のサンプルで
isParent reset?
にチェックをした場合、!fileChooser.getFileSystemView().isParent(fileChooser.getCurrentDirectory(), dir)==false
になるように?setSelectedFile
で選択ファイルをリセットする
- フォルダ名: テキストフィールドは選択ファイルとして設定したディレクトリ
- 存在しないディレクトリを
setSelectedFile
で設定するとその親ディレクトリ、親ディレクトリも存在しない場合は、OS
のデフォルトがカレントディレクトリとなる - 存在しないディレクトリを
setSelectedFile
で設定するとその親ディレクトリ、親ディレクトリも存在しない場合はOS
のデフォルトがカレントディレクトリとなる