2016-10-18 2 views
2

テキストファイルから複数の行を読み込み、それぞれのjtextフィールドに書き出すことはできますか?

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; 
    } 
} 

もう一つは、行の順序があなたのTextFieldの順序に対応していることになります。

編集
これはすべて問題なく動作することを追加する必要があります。しかしそれは非常にエレガントな解決策ではありません。 UIを変更してテキストフィールドの順序が異なるとどうなりますか?または、テキストファイルに重要な改行がありますが、UIにはTextFieldがありません。

編集2
あなたのコメントからコードがあなたの配列でのJTextFieldを入れてどのように表示されません。私の推測では、IDEを使用してGUIを作成しているので、コンストラクタにinitComomponents();コールなどが必要です。この場合、あなたのloadActionPerformed方法からラインJTextField[] textFields = new JTextField[10];を削除して、このようなあなたのコンストラクタでそれを置く:ちょうどそれを明確にするために

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.. 
    } 

編集3
、これはあなたがプログラムの実行を行うために必要なものです。 、より良い

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

private void 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){ break; } } } –

関連する問題