1
   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 ??

kskaradzinski
  • 4,954
  • 10
  • 48
  • 70
  • You will find these questions useful for the follow up questions: [How to use an ArrayList](http://stackoverflow.com/questions/2697182/how-to-use-an-array-list) and [Cannot refer to a non-final variable inside an inner class](http://stackoverflow.com/questions/1299837/cannot-refer-to-a-non-final-variable-inside-an-inner-class-defined-in-a-different) – McDowell May 04 '11 at 12:31

2 Answers2

1

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.

spot35
  • 888
  • 4
  • 9
  • I have tied something like this Component a[] = null; and in loop a[i] = t; but i got "Exception in thread "main" java.lang.NullPointerException at First.main(First.java:33)" – kskaradzinski May 04 '11 at 12:10
  • @skiwron-line - you get a NPE because `a` points to `null` - `a` is a reference to an array, not an array object in itself (this applies to all object references). If you want a data structure that can grow dynamically, use a [List](http://download.oracle.com/javase/6/docs/api/java/awt/List.html) - `List l = new ArrayList();` and `l.add(t);` – McDowell May 04 '11 at 12:24
  • @skowron-line: Please edit your question with the latest changes to the code. – Andrew Thompson May 04 '11 at 12:24
1

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.

phuibers
  • 1,239
  • 11
  • 11