hub
hub

Reputation: 101

assign text file value to dynamically created jTextFields

I have jPanel1 with jTextField1 and addTfButton. addTfButton adding dynamic tf to jPanel2. Using this code I want to load text file data to jTextField1 and dynamically created tf:

private void loadButtonActionPerformed(java.awt.event.ActionEvent evt) {   
    try {
          BufferedReader br = new BufferedReader(new FileReader ("file.txt"));
          String str=null;

          while( (str = br.readLine()) !=null ) {
              String[] s = str.split(":");                   
              jTextField2.setText(s[2]);

              for (int i = 0; i < jPanel2.getComponentCount(); i++) {
                  SubPanel panel = (SubPanel) jPanel2.getComponent(i);
                    JTextField tf = panel.getTf();
                    tf.setText(s[2]);
                    System.out.println(s[2]);
              }
           }
     }

Mu SubPanel class:

private class SubPanel extends javax.swing.JPanel {        
        SubPanel me;
        JTextField tf = new JTextField();        
    public SubPanel() {
            super();
            me = this;                      
            me.setLayout(new javax.swing.BoxLayout(me, javax.swing.BoxLayout.LINE_AXIS));          
            tf.setFont(new java.awt.Font("Arial", 0, 16));
            me.add(tf);          
    } 

    public JTextField getTf(){
            return tf;
    }
}

The contents of file.txt:

Name:1:a
Name:2:b
Name:3:c

To dynamic jTextfields I want to load values: b, c. But with above code dynamically added tf reading just s[2] of end row, i.e. c:

a //jTextField2
c //dynamically created tf[1]
c //dynamically created tf[2]

I w'd like to ask how to assign s[2] value to dynamically created jTextFields?

Upvotes: 0

Views: 167

Answers (2)

Jernej K
Jernej K

Reputation: 1694

If you want to set each value you read separately, then you would have to take the for loop out of the while loop.

BufferedReader br = new BufferedReader(new FileReader ("file.txt"));
String str=null;
// I set the counter to zero
int i = 0;
while( (str = br.readLine()) !=null ) {

    String[] s = str.split(":");                   
    jTextField2.setText(s[2]);

    SubPanel panel = (SubPanel) jPanel2.getComponent(i);
    JTextField tf = panel.getTf();
    tf.setText(s[2]);
    System.out.println(s[2]);
    // for each new line we take the next SubPanel
    i++;
}

This would allow you to set each value you read in a separate SubPanel. First try that, then we can go from there.

UPDATE

From what you told me, you would like to save the first value in the jTexField2 instance and the rest of the values in the tf instances of each SubPanel. Then my solution would be the following:

BufferedReader br = new BufferedReader(new FileReader ("file.txt"));
String str=null;
// I set the counter to zero
int i = 0;
while( (str = br.readLine()) !=null ) {

    String[] s = str.split(":");
    if(i == 0) {
        // First line of every file would be added to the TextField2 instance                
        jTextField2.setText(s[2]);

    } else {
        // Every additional line would be added to the tf instance
        // we have to substract -1 to i in order to get the first SubPanel
        SubPanel panel = (SubPanel) jPanel2.getComponent(i - 1);
        JTextField tf = panel.getTf();
        tf.setText(s[2]);
    }
    System.out.println(s[2]);
    // for each new line we take the next SubPanel
    i++;
}

Upvotes: 1

Sergey Chechenev
Sergey Chechenev

Reputation: 79

Looks like second and third SubPanel are containing the same JTextField object. But I haven't seen entire code.

Upvotes: 0

Related Questions