Jeric Marquez Jeric Marquez - 9 days ago 4
Java Question

Is it possible to read multiple lines from textfile and export it to their respective jtextfields?

private void loadActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
FileReader reader = new FileReader("reload.txt");
BufferedReader br = new BufferedReader(reader);,null);,null);,null);,null);,null);,null);,null);,null);,null);,null);,null);,null);

}catch(IOException e) {


Is it even possible to put each of them to a certain textfield?Like 12 to jtextfield1,10 to jtextfield2 and so on...I've tried some tutorials and can't really figure it out.


You could put all your textFields in an array and then iterate over that array while you are reading the text file. Like this:

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

However, in this case you have to make sure that there is a textfield for every line or that you do not read more lines than there are textfields.

for example:


Another thing to notice will be, that the order of the lines has to correspond to the order of your textFields.

I have to add, that all this can work without any problem. But it is not a very elegant solution. What happens when you change your UI and the Textfields are in a different order? Or there is an important new line in your textfile but no TextField in your UI?

Edit 2
The code from your comment does not show how you put the JTextFields in your array. My Guess is that you are using some IDE to create the GUI, so you should have a initComomponents(); call or something in your constructor. In this case, remove the line JTextField[] textFields = new JTextField[10]; from your loadActionPerformed method and put it in your constructor like this:

public class MyClass{

     private JTextField[] textFields;

     public MyClass(){
         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..

Edit 3
Just to make it clear, this is what you need to make the program run. Let's say your class is called MyClass then it could look like this:

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"))){
            if(line == textFields.length){
    }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