Swing/FileDialog のバックアップ(No.1)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/FileDialog へ行く。
- category: swing folder: FileDialog title: FileDialogでファイルを選択する tags: [FileDialog] author: aterai pubdate: 2019-07-22T16:47:18+09:00 description: FileDialogを使用してファイルを選択します。 image: https://drive.google.com/uc?id=160UZBhnWm9tvyAZVDT76viFOmqNicVDQ
概要
FileDialogを使用してファイルを選択します。
Screenshot

Advertisement
サンプルコード
JButton button1 = new JButton("FileDialog(Frame)"); button1.addActionListener(e -> {
Frame frame = JOptionPane.getFrameForComponent(this);
FileDialog fd = new FileDialog(frame, "title");
fd.setTitle("FileDialog(Frame frame, String title)");
fd.setDirectory(System.getProperty("user.home"));
fd.setVisible(true);
if (fd.getFile() != null) {
File file = new File(fd.getDirectory(), fd.getFile());
append(file.getAbsolutePath());
}
});
解説
FileDialog(Frame)- new FileDialog(Frame frame, String title)で
FileDialogを作成 FileDialog#setVisible(true)でFileDialogを開く前の場合、FileDialog#setTitle(...)でタイトルを変更可能FileDialogにWindowListenerを追加してwindowOpened後にFileDialog#setTitle(...)でタイトル変更は不可FileDialog#setLocation(...)などで表示位置の変更は不可- 値は変更されるが、実際の表示位置には反映されない
FileDialogにWindowListenerは有効だが、WindowStateListenerは無効?FileDialog#getFile()メソッドで選択されたファイルの名前が文字列で取得可能- フルパスが必要な場合は
FileDialog#getDirectory()で親ディレクトリを取得てnew File(String parent, String child)などでFileを生成し、File#getAbsolutePath()メソッドを使用する
- new FileDialog(Frame frame, String title)で
FileDialog(Dialog)- new FileDialog(Dialog dialog, String title)で
FileDialogを作成 new FileDialog(Frame)で作成した場合との違いは不明
- new FileDialog(Dialog dialog, String title)で