2011-03-06 7 views
0

JComboBox in Java 외부 파일에서 항목을 추가 할 때 도움이 필요합니다.은 다음외부 파일에서 JComboBox에 항목을 추가하는 방법은 무엇입니까?

//Loading the Names: 

     File Names_File = new File("Data" + File.separator + "Names.txt"); 
     FileInputStream fis = null; 
     BufferedInputStream bis = null; 
     DataInputStream dis = null; 

     String str_Data = ""; //For storing the input from the file. 

     try 
     { 
      fis = new FileInputStream(Names_File); 

      // Here BufferedInputStream is added for fast reading. 
      bis = new BufferedInputStream(fis); 
      dis = new DataInputStream(bis); 


      str_Data = dis.readLine(); //Reading the line from the file. 

      StringTokenizer st = new StringTokenizer(str_Data); //Tokenizing the line. 

      //The below line adds only one item. The objective is adding all the items. 

      //*** Requesting help here *** 

      cmb_Name.addItem(st.nextToken("|")); 

      //*** Requesting help here *** 

      // Disposing and closing all the resources after using them. 
      fis.close(); 
      bis.close(); 
      dis.close(); 
     } 

     catch (FileNotFoundException e) 
     { 
      System.err.println("Error: File not found!"); 
      JOptionPane.showMessageDialog(null, "Error: File not found!", "Error Message", 
              JOptionPane.ERROR_MESSAGE); 
     } 

     catch (IOException e) 
     { 
      System.err.println("Error: Unable to read from file!"); 
      JOptionPane.showMessageDialog(null, "Error: Unable to read from file!", "Error Message", 
              JOptionPane.ERROR_MESSAGE); 
     } 

주로 내 외부 파일의 형식은 다음과 같습니다 : : 여기

지금까지 내 코드 | 로버트 |

제임스 앨리스

이름을 분리 에 의해 "|" 기호.

위의 코드는이 예제에서 하나의 요소 ("James") 만 추가합니다. 나는이 세 가지를 루프 또는 무언가에 추가해야 할 필요가있다. 아이디어는 얼마나 많은 이름이 내 외부 파일에 있는지 확실히 알지 못한다는 것입니다. 따라서 간단한 카운터를 사용하면 도움이되지 않습니다.

모든 의견을 매우 높이 평가합니다. 당신의 도움에 미리

감사

답변

1

이 시도 :

StringTokenizer st = new StringTokenizer(str_Data); //Tokenizing the line. 

    while(st.hasMoreTokens()) { 
     cmb_Name.addItem(st.nextToken("|")); 
    } 
관련 문제