Muhammad Shuja
Muhammad Shuja

Reputation: 672

How do I get data from dynamically generated text fields in Java?

I have generated N text fields using for loop (N is the number of inputs entered by user). I want to get data entered by user in those text fields and save it in an ArrayList. I don't know the name of those fields so is there any way to get the text fields names? Or how do I store data from N textfields to an ArrayList and use values from that ArrayList. Here is my code:

public class FCFS extends JFrame implements ActionListener{
    JButton btn1,btn2,b;
    JTextField tf1,tf2;
    JPanel p;
    Container c;
    ArrayList arr=new ArrayList();
    public FCFS(){
        //super("FCFS");
        c=getContentPane();
        p=new JPanel();
        JLabel lbl1=new JLabel("Enter number of processes:");
        p.add(lbl1);
        tf1=new JTextField(20);
        p.add(tf1);
        btn1=new JButton("Enter");
        btn1.addActionListener(this);
        p.add(btn1);
        c.add(p);

    }
    public static void main(String args[]){
        FCFS obj=new FCFS();
        obj.setVisible(true);
        obj.setSize(300,500);
        obj.setDefaultCloseOperation(3);

    }
    @Override
    public void actionPerformed(ActionEvent ae){
        if(ae.getSource()==btn1){
            p.removeAll();
            int a=Integer.parseInt(tf1.getText());
            for(int i=1;i<=a;i++){
                p.add(new JLabel("Enter Burst for Process "+i+":"));
                tf2=new JTextField(20);
                p.add(tf2);
                arr.add(tf2.getText());
                c.add(p);
                revalidate();
                repaint();
            }
            b=new JButton("Show");
            p.add(b);
            c.add(p);
            revalidate();
            repaint();
            b.addActionListener(this);
        }
        else if(ae. getSource()==b){
            System.out.println("Hello");
            System.out.println(arr);
        }
    }
}

Upvotes: 0

Views: 1305

Answers (1)

Uri Agassi
Uri Agassi

Reputation: 37409

You don't need the name of the fields, you need the fields themselves. You can iterate over the Panel, and find all the text fields, or you can keep them in the array, and then extract their values when you want to use it:

@Override
public void actionPerformed(ActionEvent ae){
    if(ae.getSource()==btn1){
        p.removeAll();
        int a=Integer.parseInt(tf1.getText());
        for(int i=1;i<=a;i++){
            p.add(new JLabel("Enter Burst for Process "+i+":"));
            tf2=new JTextField(20);
            p.add(tf2);
            arr.add(tf2);
        }
        b=new JButton("Show");
        p.add(b);
        c.add(p);
        revalidate();
        repaint();
        b.addActionListener(this);
    }
    else if(ae.getSource()==b){
        System.out.println("Hello");
        ArrayList texts=new ArrayList();
        for (Object textField : arr) {
            texts.add(((JTextField)textField).getText();
        }
        System.out.println(texts);
    }
}

Upvotes: 1

Related Questions