2014-11-03 57 views
0

所以,我試圖調用getFile()來打開一個JFileChooser。它第一次被調用,但不是第二次。我的JFileChooser在第二次使用時不起作用

我有一段時間(!完成)循環作爲我的主菜單中的一個菜單,其中一個選項獲取該文件並執行一些操作。它第一次正常工作,但第二次嘗試從菜單執行時,它卡在if(returnVal == JFileChooser.APPROVE_OPTION)。更奇怪的是,當我調試這條線時,它會多次工作。我也積極的對話框沒有隱藏在第二次的地方。我能找到的唯一懷疑是事件派發線程中正在發生的事情,但這超出了我的理解水平。那麼,我想我對JFileChoosers有些誤解?

public void getFile() 
throws FileNotFoundException, IOException 
{ 
    JFileChooser myChooser = new JFileChooser(); 
    FileNameExtensionFilter filter = new FileNameExtensionFilter("txt files", "txt"); 
    myChooser.setFileFilter(filter); 
    int returnVal = myChooser.showOpenDialog(null); 
    if(returnVal == JFileChooser.APPROVE_OPTION) { 
     File myFile=myChooser.getSelectedFile(); 
     this.myFile=myFile;  
     String tempFileName= myFile.getName(); 
     fileName= tempFileName.substring(0, (tempFileName.length()-4)); 
     System.out.println("File Picked: " +fileName); 
     this.fileName=fileName; 
    } 
} 

編輯:

好了,這是什麼,再現了問題:

測試儀:

import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.Scanner; 


public class Test { 
    public static void main(String[] args) 
throws FileNotFoundException, IOException 
    { 

    Scanner myScanner = new Scanner(System.in); 

     boolean done = false; 
     while (!done) 
     { System.out.println(""); 
      System.out.println("1 - GetFile"); 
      System.out.println("2 - Exit"); 
      int choice= Integer.parseInt(myScanner.nextLine()); 

      if (choice ==1) 
      { System.out.println("GetFile ");  
      Chooser myChooser=new Chooser(); 
      myChooser.getFile(); 
      System.out.println("done"); 
      } 
       else if(choice ==2) 
       { System.out.println("Good Bye"); done=true;}  
         else System.out.println("Invalid choice"); 

     } while (!done); 

         System.exit(0); 
    } 
} 

計劃:

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import javax.swing.JFileChooser; 


public class Chooser { 
public void getFile() 
throws FileNotFoundException, IOException 
{ 
    JFileChooser myChooser = new JFileChooser(); 
    int returnVal = myChooser.showOpenDialog(null); 
    if(returnVal == JFileChooser.APPROVE_OPTION) { 
     System.out.println("loop marker"); 
     File myFile=myChooser.getSelectedFile();  
     String tempFileName= myFile.getName(); 
     System.out.println("File Picked: " +tempFileName); 
    } 
} 

} 
+0

爲了更好地幫助越早,張貼[MCVE]( http://stackoverflow.com/help/mcve)(最小完整可驗證示例)。 – 2014-11-03 00:04:06

+1

*「while(!done)loop作爲我主菜單中的菜單」*現在我只是害怕。將一段斷斷續續的代碼片段傾倒在我們身上,期待我們神奇地知道它是如何工作的,以及它的錯在哪裏就是神奇的想法。考慮提供一個[可運行的示例](https://stackoverflow.com/help/mcve),它可以證明你的問題。這將導致更少的混淆和更好的響應 – MadProgrammer 2014-11-03 00:04:53

+2

這聽起來像是你阻塞了事件調度線程。請參閱[併發中的Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) – MadProgrammer 2014-11-03 00:05:47

回答

0

我無法重現你的具體問題。該代碼適用於我;然而,它仍然是壞的,因爲從主線程中使用Swing組件是錯誤的。

嘗試切換到事件調度線程你之前做別的(這是由你的異常需要重新拋出作出遺憾的是較爲凌亂):

public static void main(final String[] args) throws FileNotFoundException, IOException { 
    if (!SwingUtilities.isEventDispatchThread()) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        main(args); 
       } catch (Exception e) { 
        throw new RuntimeException(e); 
       } 
      } 
     }); 
     return; 
    } 

    ... 
} 
相關問題