Created
July 15, 2014 09:04
-
-
Save funatsufumiya/f761f644390dde5e4b5a to your computer and use it in GitHub Desktop.
JavaでOS独自のファイルダイアログを表示する方法 ref: http://qiita.com/atmarksharp/items/f047dccee4c2d8b2c28a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import javax.swing.*; | |
import java.io.File; | |
class NativeFileDialog { | |
public static void print(String s){ | |
System.out.print(s); | |
} | |
public static void println(String s){ | |
System.out.println(s); | |
} | |
public static void main(String[] args) { | |
println(""); | |
showAWTDialog(); | |
showSwingDialog(); | |
System.exit(0); | |
} | |
static void showAWTDialog(){ | |
FileDialog dialog = new FileDialog( | |
(Frame)null, | |
"FileDialog (AWT)", | |
FileDialog.LOAD); | |
dialog.setVisible(true); | |
String filename = dialog.getFile(); | |
print("FileDialog (AWT): "); | |
if (filename != null){ | |
println(filename); | |
}else{ | |
println("cancelled"); | |
} | |
} | |
static void showSwingDialog(){ | |
JFileChooser filechooser = new JFileChooser("FileDialog (Swing)"); | |
int selected = filechooser.showOpenDialog(null); | |
print("JFileChooser (Swing): "); | |
if (selected == JFileChooser.APPROVE_OPTION){ | |
File file = filechooser.getSelectedFile(); | |
String filename = file.getName(); | |
println(filename); | |
}else{ | |
println("cancelled"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javac NativeFileDialog.java | |
java NativeFileDialog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FileDialog (AWT): filedialog-awt.png | |
JFileChooser (Swing): cancelled |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment