Vega512
Vega512

Reputation: 321

How to access and store input when new JTextFields are created

Im trying to create a GUI that mimics an organizer/scheduler but im having an issue when i insert more fields for the user to input their task. In my code i have a button specified for adding more fields and another to submit them, but if the user chooses to add more fields i can only access the current fields but not the previous ones when they enter submit. How can i access previous fields?

import javax.swing.JTextField;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;  

class Organizer extends JPanel implements ActionListener
{
     protected JTextField dateText,taskText, priorityText;
     Button addField;
     Button submit;
     JPanel panel;
     Vector<String> Dates = new Vector();
     Vector<String> Tasks = new Vector();
     Vector<String> Priority = new Vector();


     public void createWindow()
     {
        JFrame frame = new JFrame ("Organizer");
        panel = new JPanel();
        frame.setVisible(true);
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);

        addField = new Button("(+)");
        addField.addActionListener(this);
        panel.add(addField);

        submit = new Button ("submit");
        submit.addActionListener(this);
        panel.add(submit);

        addFields();
     }

     public void addFields()
     {
        dateText = new JTextField("Enter Date(mon/dd/yyyy)",30);
        taskText = new JTextField("Enter Task",30);
        priorityText = new JTextField("Rate importance(10-high,1-low)",30);

        panel.add(dateText);
        panel.add(taskText);
        panel.add(priorityText);
     }

     public void actionPerformed(ActionEvent evt)
     {
        if(evt.getSource() == addField)
        { 
          addFields();
          panel.revalidate();
          panel.repaint(); 
        }

        if(evt.getSource() == submit)
        {
           String temp = dateText.getText(); 
           System.out.println(temp);

           //Dates.addElement(temp);
           //System.out.println(Dates);
        }
     }

     public static void main (String[] args)
     {
        Organizer Org = new Organizer();
        Org.createWindow();
     }
}

Upvotes: 1

Views: 637

Answers (2)

Vishal K
Vishal K

Reputation: 13066

You need to store group of JTextFields added to JPanel each time addField JButton is clicked to an ArrayList. And while submitting you could retrieve the same. Here is the rough demo of what you code should look like:

class FieldsGroup //Represents each group of JTextFields
{
    JTextField dateText;
    JTextField taskText;
    JTextField priorityText;
    public FieldsGroup(JTextField dateText,JTextField taskText,JTextField priorityText)
    {
        this.dateText = dateText;
        this.taskText = taskText;
        this.priorityText = priorityText;
    }
    public String getDateText()
    {
        return dateText.getText();
    }
    public String getTaskText()
    {
        return taskText.getText();
    }
    public String getPriorityText()
    {
        return priorityText.getText();
    }
}

Then within your Organizer class you should create an ArrayList object as follows:

     ArrayList<FieldsGroup> groups = new ArrayList<FieldsGroup>();//Create an ArrayList

Change the addFields method in following way:

     public void addFields()
     {
        dateText = new JTextField("Enter Date(mon/dd/yyyy)",30);
        taskText = new JTextField("Enter Task",30);
        priorityText = new JTextField("Rate importance(10-high,1-low)",30);

        panel.add(dateText);
        panel.add(taskText);
        panel.add(priorityText);
        groups.add(new FieldsGroup(dateText,taskText,priorityText));//add each group of JTextFields to ArrayList object groups.
     }

And finally change the submit button actionPerformed as follows:

        if(evt.getSource() == submit)
        {
            for (FieldsGroup gr : groups)
            {
                System.out.println(gr.getDateText());
                System.out.println(gr.getTaskText());
                System.out.println(gr.getPriorityText());
                System.out.println("-----------------------------");
            }
       }

Upvotes: 2

cproinger
cproinger

Reputation: 2288

i would suggest to extract a new panel-class containing the dateText,taskText, priorityText and keep a List around for the panels you added.

Upvotes: 0

Related Questions