2016-10-18 10 views
2

텍스트 파일에서 여러 줄을 읽고 각각의 jtextfield로 내보낼 수 있습니까?

private void loadActionPerformed(java.awt.event.ActionEvent evt) {          
    // TODO add your handling code here: 
    try{ 
     FileReader reader = new FileReader("reload.txt"); 
     BufferedReader br = new BufferedReader(reader); 
     koontf.read(br,null); 
     baamtf.read(br,null); 
     sachitf.read(br,null); 
     fakertf.read(br,null); 
     phonsekaltf.read(br,null); 
     lauretf.read(br,null); 
     yeontf.read(br,null); 
     aguerotf.read(br,null); 
     agnistf.read(br,null); 
     lokitf.read(br,null); 
     lawliettf.read(br,null); 
     ryuzakitf.read(br,null); 
     br.close(); 
     koontf.requestFocus(); 
     baamtf.requestFocus(); 
     sachitf.requestFocus(); 
     fakertf.requestFocus(); 
     phonsekaltf.requestFocus(); 
     lauretf.requestFocus(); 
     yeontf.requestFocus(); 
     aguerotf.requestFocus(); 
     agnistf.requestFocus(); 
     lokitf.requestFocus(); 
     lawliettf.requestFocus(); 
     ryuzakitf.requestFocus(); 

    }catch(IOException e) { 

    } 
}          

는 특정 텍스트 필드에 각각 넣어도 가능합니까? (12)처럼 jtextfield2하고 등등 ... 내가 할 수없는 몇 가지 튜토리얼을 시도했습니다 jtextfield1,10하기 정말로 그것을 알아 낸다.

+0

더 나은하는 JSON으로 지속 다시 설정하고 각 필드에 읽을 :의이 클래스는 MyClass이 다음은 다음과 같이 할 수라고 가정 해 봅시다. 참조는 https://www.mkyong.com/java/json-simple-example-read-and-write-json/ – subash

답변

2

모든 textField를 배열에 넣은 다음 텍스트 파일을 읽는 동안 해당 배열을 반복 할 수 있습니다. 이처럼 :

JTextField[] textFields = new JTextField[10]; 
// ... init your textFields here 

int line =0; // first line will be first textfield and so on 
Scanner scanner = new Scanner(new File("reload.txt")); // use Scanner instead of FileReader, it's easier :) 
while(scanner.hasNextLine()){ // as long as you did not reach the end of the file 
    textFields[line++].setText(scanner.nextLine()); // get the next line and put it in the respective textfield 
} 

그러나이 경우 당신은 모든 라인의 텍스트 필드가 있다고하거나 텍스트 필드가보다 더 많은 라인을 읽지 않는 있는지 확인해야합니다. 예를 들어

:

while(.....){ 
    .... 
    if(line==textFields.length){ 
     break; 
    } 
} 

라인의 순서는 당신의 텍스트 필드의 순서에 해당하는 것이 될 것입니다 주목해야 할 또 다른 것은.

나는이 모든 문제없이 작동 할 수있는, 추가해야 할
편집
. 그러나 이것은 매우 우아한 해결책은 아닙니다. UI를 변경하고 텍스트 필드의 순서가 다른 경우 어떻게됩니까? 또는 텍스트 파일에 중요한 새 줄이 있지만 UI에 TextField가 없습니까?

편집 2
당신이 당신의 배열의 JTextFields을 넣어하는 방법을 표시하지 않습니다 귀하의 코멘트에서 코드입니다. 내 생각 엔 GUI를 만들기 위해 일부 IDE를 사용하고 있으므로 생성자에서 initComomponents(); 호출 또는 다른 것이 있어야합니다. 3, 이것은 당신이 프로그램 실행을 위해 필요한
그냥 명확하게하는 것입니다

public class MyClass{ 

    private JTextField[] textFields; 

    public MyClass(){ 
     initComponents(); 
     this.textFields = new JTextField[10] // where 10 is the number of lines in your textfile AND the number of JTextFields you have in your GUI 
     // then fill the array (by hand if you like) 
     this.textField[0] = koontf; 
     this.textField[1] = baamtf; 
     // and so on.. 
    } 

편집 :이 경우, loadActionPerformed 방법에서 선 JTextField[] textFields = new JTextField[10];을 제거하고이 같은 생성자에 넣어 .

private JTextField[] textFields; // this creates your array 

public MyClass(){  // this is the constructor of your class (I don't know how it is called) 
    initComponents(); // auto generated code from NetBeans to initalize your GUI elements 
    // init your array 
    textFields = new JTextField[12]; // 12 if I counted correctly 
    // fill it 
    textFields[0] = koontf; 
    textFields[1] = baamtf; 
    textFields[2] = sachitf; 
    textFields[3] = fakertf; 
    textFields[4] = phonsekaltf; 
    textFields[5] = lauretf; 
    textFields[6] = yeontf; 
    textFields[7] = aguerotf; 
    textFields[8] = agnistf; 
    textFields[9] = lokitf; 
    textFields[10] = lawliettf; 
    textFields[11] = ryuzakitf; 
} 

private void loadActionPerformed(java.awt.event.ActionEvent evt){ 
    int line = 0; 
    try(Scanner scanner = new Scanner(new File("reload.txt"))){ 
     while(scanner.hasNextLine()){ 
      textFields[line++].setText(scanner.nextLine()); 
      if(line == textFields.length){ 
       break; 
      } 
     } 
    }catch(FileNotFoundException ex){ 
     Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    koontf.requestFocus(); // you can only call request focus on one element at a time (it does not make sense to call it on all textfields 
} 
+1

에서 볼 수 있습니다. 'Array' 대신'Collection'을 사용하는 것을 선호합니다. 크기가 더 쉽게 변경 될 수 있습니다. – slartidan

+0

좋은 지적! 요소의 순서가 변경되지 않기 때문에 내 개인적인 선호는'ArrayList '입니다 (이 경우) – GameDroids

+0

무효 loadActionPerformed (java.awt.event.ActionEvent evt) { // 여기에 처리 코드를 추가하십시오 : JTextField [] textFields = 새로운 JTextField [10]; int line = 0; 스캐너 스캐너 = 새 스캐너 ("reload.txt"); while (scanner.hasNextLine()) { textFields [line ++]. setText (scanner.nextLine()); if (line == textFields.length) { 휴식; } } } –