2017-06-21 10 views
0

私はコードに何が問題なのか分かりません。私はそれを理解するのを助けることができますか?なぜ私のファイル選択者はダイアログを開きません

private void doOpenFile() { 
    int result = myFileChooser.showOpenDialog(this); 

    if (result == JFileChooser.APPROVE_OPTION) { 
     Path path = myFileChooser.getSelectedFile().toPath(); 

     try { 
      String contentString = ""; 

      for (String s : Files.readAllLines(path, StandardCharsets.UTF_8)) { 
       contentString += s; 
      } 

      jText.setText(contentString); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

private void doSaveFile() { 
    int result = myFileChooser.showSaveDialog(this); 

    if (result == JFileChooser.APPROVE_OPTION) { 
     // We'll be making a mytmp.txt file, write in there, then move it to 
     // the selected 
     // file. This takes care of clearing that file, should there be 
     // content in it. 
     File targetFile = myFileChooser.getSelectedFile(); 

     try { 
      if (!targetFile.exists()) { 
       targetFile.createNewFile(); 
      } 

      FileWriter fw = new FileWriter(targetFile); 

      fw.write(jText.getText()); 
      fw.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+1

[何のエラー/例外がありましたか](// stackoverflow.com/help/mcve)?その詳細は、私たちが問題を診断するのに役立ちます。 –

+0

同じ質問を複数回書くことで答えが得られません。実際に出力やエラーとして何が得られているのかについて、より詳細な情報を提供する必要があります。 –

答えて

0

は、私はあなたのmyFileChooserを使用しないと、あなたがそれらをどのようにインスタンス化しないかどうかはわかりませんが、このコードはうまく機能:

ここ
public class ForTestApplication { 


    public static void main(String[] args) { 

    Window window = new Window(); 

    window.setVisible(true); 

    window.showFileChooser(); 

    } 


    static class Window extends JFrame { 

    JFileChooser jFileChooser = new JFileChooser(); 

    public void showFileChooser() { 

     jFileChooser.showDialog(this, "Just for test"); 

    } 

    } 

} 

はスクリーンショットです:enter image description here

、より多くを提供してください。何が起こっているのか把握するコード。

関連する問題