次のコードスニペットでjava.awt.FileDialogを呼び出す際に問題が発生しました。 OS Xのスピナーは常に回転し、何も変更されて(Finderが開きません)OS Xエルキャピタン10.11.3 Java 8 FileDialogが開かない
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("CSV Parser");
Button button = new Button();
button.setText("Import Translations");
button.setOnAction(event -> {
String openFile = openFile();
System.out.println("Open file " + openFile);
});
VBox vbox = new VBox();
vbox.setPadding(new Insets(10));
vbox.setSpacing(8);
vbox.getChildren().add(button);
primaryStage.setScene(new Scene(vbox));
primaryStage.show();
}
public static String openFile() {
JFrame parentFrame = getJFrame("JFrame");
String osName = System.getProperty("os.name");
if (osName.toLowerCase().contains("mac")) {
FileDialog fileDialog = new FileDialog(parentFrame);
FilenameFilter csvFilter = (dir, name) -> name.endsWith(".csv");
fileDialog.setFilenameFilter(csvFilter);
fileDialog.setFile("*.csv");
fileDialog.setMode(FileDialog.LOAD);
String dirHome = System.getProperty("user.home");
fileDialog.setDirectory(dirHome);
fileDialog.setVisible(true);
boolean isShowing = fileDialog.isShowing();
if (isShowing) {
File fileToOpen = new File(fileDialog.getFile());
String path = fileToOpen.getAbsolutePath();
parentFrame.dispatchEvent(new WindowEvent(parentFrame, WindowEvent.WINDOW_CLOSING));
return path;
} else {
parentFrame.dispatchEvent(new WindowEvent(parentFrame, WindowEvent.WINDOW_CLOSING));
return null;
}
}
return null;
}
private static JFrame getJFrame(String name) {
JFrame parentFrame = new JFrame(name);
parentFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
ex.printStackTrace();
}
return parentFrame;
}
public static void main(String[] args) {
launch(args);
}
}
私はちょうど適切な拡張子(フォルダではなく)を使用してファイルを選択する機能を持っている必要があり、ダイアログの外観はありません。双方向ですが、私は外部ライブラリなしでそれを実装したい。 私は何か助けに感謝します。
* "とスピナーがスピンしている" *スピナーとは何ですか? 'FileDialog'の代わりに' JFileChooser'を使わないのはなぜですか?もっと早く助けを求めるには、[MCVE]または[短く、自己完結型の正しい例](http://www.sscce.org/)を投稿してください。 –
@AndrewThompson FileDialogではなくJFileChooserでは動作しません。多くの人がSOの質問でJFileChooserの問題を報告しました。私はFileDialogに切り替えることにしました。 – Ray