FileDialogでファイルを選択する
Total: 6477, Today: 1, Yesterday: 1
Posted by aterai at
Last-modified:
Summary
FileDialogを使用してファイルを選択します。
Screenshot

Advertisement
Source Code Examples
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());
}
});
View in GitHub: Java, KotlinDescription
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)で
Reference
- FileDialog (Java Platform SE 8)
- How do I center a java.awt.FileDialog on the screen - Stack Overflow