Skip to content

Instantly share code, notes, and snippets.

@funatsufumiya
Created July 15, 2014 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funatsufumiya/f761f644390dde5e4b5a to your computer and use it in GitHub Desktop.
Save funatsufumiya/f761f644390dde5e4b5a to your computer and use it in GitHub Desktop.
JavaでOS独自のファイルダイアログを表示する方法 ref: http://qiita.com/atmarksharp/items/f047dccee4c2d8b2c28a
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");
}
}
}
javac NativeFileDialog.java
java NativeFileDialog
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