다음 코드 스 니펫으로 java.awt.FileDialog를 호출하는 동안 문제가 발생했습니다.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);
}
}
내가 적절한 확장 (안 폴더)가있는 파일을 선택 할 수있는 능력을 가지고 단지 필요 OS X의 회 전자 끊임없이 회전하고 아무것도 변경 (파인더가 열리지 않습니다), 대화 상자의 모양은 없습니다 이중 감각,하지만 난 외부 lib없이 구현 싶어요. 도움을 주시면 감사하겠습니다.
* "및 회 전자가 회전 중"* 회 전자는 무엇입니까? 왜'FileDialog' 대신'JFileChooser'를 사용하지 않겠습니까? 더 나은 도움을 받으려면 [MCVE] 또는 [단락, 자체 포함, 올바른 예] (http://www.sscce.org/)를 게시하십시오. –
@AndrewThompson FileDialog가 아닌 JFileChooser에서는 작동하지 않습니다. 많은 사람들이 그래서 FileDialog로 전환하기로 결정한 이유에 대해 JFileChooser에 관한 문제를 질문했습니다. – Ray