0

So I need to make a Java program that can read in user input in text-fields. I was able to set up the array of text fields but reading in the input and storing the data in a new array is troubling me greatly. I made a listener for the buttons and I just need to figure out how to store the information entered in the textField array into the grade array so I can perform calculations on the grades. I am new to this site and appreciate the help

//an Array for test scores and one to hold the input grades
  JTextField[] testScores;
  double[] grade;

/**
    Constructor
*/

public StatisticsCalculator()
{
    //Display a Title
    setTitle("JP Stearns");

    //Specify the action for the Close button
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create a Border Layout
    setLayout(new BorderLayout());

    //Create the Custom Panels
    buildScoresPanel();
    buildStatisticsPanel();

    //Build the Button Panel
    buildButtonPanel();

    //Add the Components to the content pane
    add(scoresPanel, BorderLayout.NORTH);
    add(statisticsPanel, BorderLayout.CENTER);
    add(buttonPanel, BorderLayout.SOUTH);

    //Pack the contents of the Window to display it.
    pack();
    setVisible(true);
}

    //Create a GridLayout manger
    //with 1 row 4 columns.
    scoresPanel.setLayout(new GridLayout(1,4));

    //Create 4 text fields using an array
    testScores = new JTextField[4];

    for (int index = 0; index < testScores.length; index++)
        {
            testScores[index] = new JTextField(4);
            scoresPanel.add(testScores[index]);
        }

    //Border the panel
    scoresPanel.setBorder(BorderFactory.createTitledBorder("Test Scores"));

private void buildScoresPanel()
{
    //Create a panel for the test scores
    scoresPanel = new JPanel();

    //Create a GridLayout manger
    //with 1 row 4 columns.
    scoresPanel.setLayout(new GridLayout(1,4));

    //Create 4 text fields using an array
    testScores = new JTextField[4];

    for (int index = 0; index < testScores.length; index++)
        {
            testScores[index] = new JTextField(4);
            scoresPanel.add(testScores[index]);
        }

    //Border the panel.v
    scoresPanel.setBorder(BorderFactory.createTitledBorder("Test Scores"));
}
1
  • 2
    I don't see that you've posted your ActionListener and its actionPerformed method showing your attempt to solve this just yet. Please post this so we can see what you may be doing wrong and better understand your problem. Also please take care with your code formatting, using 4 spaces (no tabs) for each indentation and using regular consistent indentation for each level. This will greatly aid our understanding of your code. Commented Aug 11, 2014 at 19:44

1 Answer 1

1

I think more information is needed, however it would look like you can do it something like this(psuedo code):

 JButton button = new JButton("This is L");
  button.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) { 

          for(int i=0; i<testScores.length(); i++)
                grade[i]=Double.parseDouble(testScores[i].getText());
                 //then do calculations like grade[i] * 100%
             }
Sign up to request clarification or add additional context in comments.

1 Comment

I will give it a try when I get home from work thanks.

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.