2016-06-13 5 views
0

다음 코드 스 니펫으로 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없이 구현 싶어요. 도움을 주시면 감사하겠습니다.

+0

* "및 회 전자가 회전 중"* 회 전자는 무엇입니까? 왜'FileDialog' 대신'JFileChooser'를 사용하지 않겠습니까? 더 나은 도움을 받으려면 [MCVE] 또는 [단락, 자체 포함, 올바른 예] (http://www.sscce.org/)를 게시하십시오. –

+0

@AndrewThompson FileDialog가 아닌 JFileChooser에서는 작동하지 않습니다. 많은 사람들이 그래서 FileDialog로 전환하기로 결정한 이유에 대해 JFileChooser에 관한 문제를 질문했습니다. – Ray

답변

0

귀하의 대화 상자가 새 프레임에 연결되었습니다 (보이지 않습니다) ... 대화 상자의 생성자에 대한 인수 또는 대화 상자가 논리적으로 링크되어야하는 현재 프레임의 참조를 사용해야합니다.

+0

이미 여러 솔루션을 사용해 보았지만 부모 프레임이 아닌 null과 함께 작동하지 않습니다. 질문에 부모 프레임을 만드는 방법을 추가했습니다. 크로스 플랫폼으로 lookAndFeel 설정을 변경하려면 게시물을 읽었지만 도움이되지 않습니다. – Ray

+0

MCVE를 게시하십시오. –

+0

JFrame을 생성하지 마십시오. 보이지 않습니다. 대화 상자를 연결하십시오. 이것은 타당하지 않습니다. –

관련 문제