2009-11-29 55 views
0

我在使用Java JFileChooser時遇到問題,並想知道是否有人可以幫我解決問題。這可能是一件非常簡單的事情,但我不能發現有什麼問題。使用Java FileChooser導入文件

JFileChooser窗口打開罰款,當我點擊我的導入按鈕,我可以導航到任何領域,但我不能將它們讀入我的JTextFields

繼承人我JFileChooser方法:

public void importFile() { 
    JFileChooser chooser = new JFileChooser();//A 
    if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { //a 
     try { 
      BufferedReader file_in = new BufferedReader(
      new FileReader(chooser.getSelectedFile().getPath())); 
      int i = 0; 

      String name = "",hnumber = "", mnumber = "", address = ""; 

      while (((fileLines = file_in.readLine()) != null)) { 
       if (fileLines.length() > 0) { 
        i++; 
        if (i == 1) { 
         name = fileLines; 
        } else if (i == 2) { 
         hnumber = fileLines; 
        } else if (i == 3) { 
         mnumber = fileLines; 
        } else if (i == 4) { 
         address = fileLines; 

         String[] nameArray = name.split(" "); 

         Contact c = new Contact (nameArray[1], nameArray[0], 
         hnumber, mnumber, address); 
         contactList.add(c); 
         index = 0; 
        } 
       } 
      } 

      for (int j = 0; j < contactList.size(); j++) { 
       System.out.print(contactList.get(j).getname()); 
       System.out.print(" "); 
       System.out.println(contactList.get(j).getmnumber()); 
       System.out.println(contactList.get(j).gethnumber()); 
       System.out.println(contactList.get(j).getaddress()); 
       System.out.println(contactList.get(j).getsurname()); 
       System.out.println(" "); 
      } 

     } catch (IOException ioe) { 
      ioe.printStackTrace(); 
     } 
    } 
} 
+1

你試過調試過嗎? – Bozho 2009-11-29 19:05:31

+0

看不到任何JTextFields – 2009-11-29 19:08:11

回答

0

你應該使用列表或便於獲得線的一個StringBuilder。你會得到任何錯誤(S)作爲結果?調試真的有助於查看您的程序在哪裏崩潰。

這裏是我的東西放在一起給你真正的快:

public void importFile() { 
    JFileChooser chooser = new JFileChooser();//A 
    if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { //a 
     try { 
      FileReader fr = new FileReader(chooser.getSelectedFile().getPath()); 
      BufferedReader file_in = new BufferedReader(fr); 
      List lines = new List(); 
      String line = new String(""); 
      while ((line = file_in.readLine()) != null) { 
       list.add(line); 
       if (list.size() >= 3) { 
        String[] nameArray = ((String)list.get(0)).split(" "); 
        Contact c = new Contact (nameArray[1], nameArray[0], 
          (String)list.get(1), (String)list.get(2), 
          (String)list.get(3)); 
        contactList.add(c); 
       } 
       System.out.println(list.get(list.size()-1)); // Debug 
      } 
     } 
     catch (IOException ioe) { 
      ioe.printStackTrace(); 
     } 
    } 
} 

我沒有編譯所以可能有一些錯別字或這樣的......

0

它導入到稱爲數組列表您可以看到的「contactList」位於底部的第5行。所以它不會直接進入JTextFields,但無論如何我都無法使它工作。