2011-08-16 5 views
0

텍스트 파일의 각 줄을 jcomboBox에 복사하려하지만 jcomboBox의 텍스트 파일의 첫 줄만 표시합니다 ... 이유를 모르겠습니다. 무엇이 잘못되었는지 설명해 주시겠습니까?텍스트 파일의 줄을 JComboBox에 복사하는 방법은 무엇입니까?

당신이 첫 번째 라인을 읽고 파일을 닫습니다 때문입니다
(...) 
BufferedReader in; 
    String read; 

     try { 
      in = new BufferedReader(new FileReader("D:/File.txt")); 


      read = in.readLine(); 

      lines[w]=read; 

      ++w; 

      in.close(); 
     }catch(IOException e){ 
      System.out.println("There was a problem:" + e); 
     } 

    combo1 = new JComboBox(lines); 

    combo1.setPreferredSize(new Dimension(100,20)); 
    combo1.setForeground(Color.blue); 


    JPanel top = new JPanel(); 
    top.add(label); 
    top.add(combo1); 

    combo1.addActionListener(new ActionFichiers()); 

    container.add(top, BorderLayout.NORTH); 
    this.setContentPane(container); 
    this.setVisible(true);    
    } 
(...) 
+0

read = in.readLine(); lines[w]=read; 

대체?! :) – Prine

+0

자바의 루프 while while 시도 – DeyyyFF

답변

5

, 고려 :

 try { 
     in = new BufferedReader(new FileReader("D:/File.txt")); 
     while((read = in.readLine()) != null){ 
      lines[w]=read; 
      ++w; 
     } 
     in.close(); 
    }catch(IOException e){ 
     System.out.println("There was a problem:" + e); 
    } 

참고 : 나는 lines 당신은 첫 번째를 읽고

4

충분히 큰 배열을 가정 파일의 라인. 그래서 JCombobox에는 더 많은 것들이있을 수 없습니다. 당신은 잠시 동안 당신이 끝날 때까지 모든 라인을 읽어야합니다.

4

당신이 첫 번째 라인을 읽고 아마 때문에
while((read = in.readLine())!=null){ 
    lines[w++]=read; 
} 
관련 문제