0

My program starts by prompting the user to enter the number of masses they would like to orbit

public class textEvent1 implements ActionListener {    //action listener for "how many masses?"
        public void actionPerformed (ActionEvent e) {
            n = (int)(Double.parseDouble(massNumField.getText()));


            submit1.setVisible(false);          //removes text event 1 text from screen
            massNumLabel.setVisible(false);
            massNumField.setVisible(false);

Then with that information I create that number of labels and text fields like so

for(int i=1; i<=n; i++) {                                  //adds text event 2 text to the screen
                    massLabel = new JLabel("How much mass does Mass " +i+ " have? ");
                    massField = new JTextField(5);

                    xCorLabel = new JLabel("What is mass "+i+"'s starting x-coordinate?");
                    xCorField = new JTextField(5);

                    yCorLabel = new JLabel("what is mass "+i+"'s starting y-coordinate?");
                    yCorField = new JTextField(5);

                    xVelLabel = new JLabel("What is mass "+i+"'s starting x velocity?");
                    xVelField = new JTextField(5);

                    yVelLabel = new JLabel("What is mass "+i+"'s starting y velocity?");
                    yVelField = new JTextField(5);

                    add(massLabel);
                    add(massField);

                    add(xCorLabel);
                    add(xCorField);

                    add(yCorLabel);
                    add(yCorField);

                    add(xVelLabel);
                    add(xVelField);

                    add(yVelLabel);
                    add(yVelField);
                }

Now my problem is reading from these text fields with with another actionlistner (a submit button) and assigning the value entered into an array. Since I dont know how many masses there will be I dont know how many text fields there will be and each text field essentially has the same name, how do I assign say the value entered for mass2's mass when its text field has the same name as all the other text fields?

2
  • 3
    Use Arrays for each of the text fields. Commented Sep 16, 2013 at 20:46
  • For example. Commented Sep 16, 2013 at 21:15

1 Answer 1

0

Use List<JTextField> for each piece of data you need.

So add to your class the instance variables:

List<JTextField> masses, xCors, yCors, xVels, yVels;

Initialize them (ArrayLists probably work fine here), then in that actionPerformed method where you create the JTextFields, add them one by one to the Lists:

for (int i = 1; i <= n; i++) {
    massLabel = new JLabel("How much mass does mass " +i+ " have? ");
    massField = new JTextField(5);
    masses.add(massField);

    /* .. initialize all other fields similarly, adding them to the Lists */

When you want to read values from / assign values to the text fields, you can access them from the Lists using masses.get(i).

Sign up to request clarification or add additional context in comments.

5 Comments

Im having trouble collecting the data from the different lists. In the next part of my program I try to collect the proper component from a list say the masses list and assign it to a double variable to construct an object. I tried casting it as a double, using Double.parseDouble(masses.get(i).getText())......any other suggestions?
Can you give a bit more detail on the error? Also, did you make sure to still call add(massField); when creating the text fields?
Ive been having a bunch of trouble with this one. I think I found the answer here link
everything kiheru says in that answer is correct. List indices start at 0, so if you have 4 masses, their indices are at 0-3, meaning that calling masses.get(4) attempts to access an index outside the list, hence the ArrayIndexOutOfBoundsException. The enhanced foreach loop is also good advice, as is using the foreach with generics so you don't have to worry about casting
do you think it is possible to do this with just a regular for loop (assign the textfield value to a variable)? I tried doing it with a bunch of cascading for-each loops and later I found out it was only assigning the final element of each list to my final array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.