kskaradzinski
kskaradzinski

Reputation: 5084

java field value sum

   public static void main(String[] args)
    {
        JFrame frame = new JFrame("test");
        JPanel panel = new JPanel();

        Component a[] = null;

        for(int i=0; i<3; i++)
        {
            final JTextField t = new JTextField();
            t.setColumns(5);

            a[i] = t;

            t.addKeyListener(new KeyListener() {

                public void keyReleased(KeyEvent e) 
                {

                }

                public void keyTyped(KeyEvent e) {}
                public void keyPressed(KeyEvent e) {}
            });
            panel.add(t);
        }

        JTextField sum = new JTextField();
        sum.setColumns(5);
        panel.add(sum);

        JScrollPane spanel = new JScrollPane(panel);
        frame.add(spanel);
        frame.setSize(800, 600);
        frame.setVisible(true);
    }

How can I sum value after pressing key i each field, from all fields which are displayed in loop ??

Upvotes: 1

Views: 1256

Answers (2)

phuibers
phuibers

Reputation: 1269

Since you use a fixed number of text fields (3 in your code sample above), I would use an array to keep reference to them and calculate the sum of the values written in them when a key is pressed. You can then loop over the array (with size 3 or another size if you choose to have more text fields) and sum all the numbers in the text fields. Extending the code you already provided, it would look something like:

public static void main(String[] args)
{
     JFrame frame = new JFrame("test");
     JPanel panel = new JPanel();
     int nrOfTextFields = 3;
     JTextField[] textFields = new JTextField[nrOfTextFields];
     int sumOfallTextFields = 0;
     for(int i=0; i<nrOfTextFields; i++)
     {
         final JTextField t = new JTextField();
         t.setColumns(5);
         t.addKeyListener(new KeyListener()
         {
              public void keyReleased(KeyEvent e)
              {
                     sumOfAllTextFields = 0;
                     for(int i=0; i<nrOfTextFields; i++)
                     {
                            int textFieldValue = Integer.parseInt(textFields[i].getString());
                            sumOfAllTextFields += textFieldValue;
                     }
              }
              public void keyTyped(KeyEvent e) {}
             public void keyPressed(KeyEvent e) {}
         });
         panel.add(t);
     }
     JTextField sum = new JTextField();
     sum.setColumns(5);
     panel.add(sum);
     JScrollPane spanel = new JScrollPane(panel);
     frame.add(spanel);
     frame.setSize(800, 600);
     frame.setVisible(true);
 } 

This is assuming that the numbers which are entered in the text fields are integers. There is also no check to see if the user entered valid numbers (e.g. no text or symbols) in the text fields.

Upvotes: 1

spot35
spot35

Reputation: 888

You could try adding all the text fields to an array first and then when a key is pressed, iterate over the array and sum the values.

Upvotes: 1

Related Questions