0

I have added set of integers to a JTextArea for each button click. what exactly I want is that I want to add all the integers and display in a separate JTextArea,Also I want to ask whether we can access the value of a variable within an action listener outside the action listener.

Here is the code:

private ActionListener listener = new ActionListener() {  
@Override
public void actionPerformed(ActionEvent evt) {  
    if(evt.getActionCommand().equals(t.getText()))
    {
        onec=one.calone(n);    
        td.append(Double.toString(onec));
        td.append("\n");
    }
    res=Integer.parseInt(td.getText());
}

};

When the user presses button 't' It will keep on adding the integer 'onec' to textarea 'td' using append method.And I have stored the result from the action listener into a variable 'res' of double datatype.

private ActionListener listener2 = new ActionListener() {  
    @Override
    public void actionPerformed(ActionEvent e) {  
        if(e.getActionCommand().equals(tot.getText()))
        {
            totd.setText(Double.toString(res));
        }
    }
};

When the user clicks the button 'tot',It should add all the integers in the textarea 'td' and display it in the textarea 'totd'. This code is not working. Please help me this is the last part of my project.

nachokk
  • 14,363
  • 4
  • 24
  • 53
irfan
  • 1
  • 1
  • What do you mean by not working? – Stu Whyte Aug 01 '13 at 17:17
  • I mean to say it is not showing any error but it is displaying 0.0 in the textarea totd which is the value I have initialized the variable res. – irfan Aug 01 '13 at 17:21
  • @irfan : Please explain a bit more, as to what `calone(...)` is doing and what it is returning. Now after capturing what is being returned, it seems you wanted to append that to one `JTextArea` and then when you click a button, you simply want to add all the values in this `JTextArea` and then display the result in some another `JTextArea`. If this is not what you mean, then please put more effort into explaining your situation. – nIcE cOw Aug 01 '13 at 17:34
  • Something like [this](http://stackoverflow.com/a/13919878/230513)? – trashgod Aug 01 '13 at 17:59

2 Answers2

1

As I don't know what is not working - it would of been good if you explained more clearly - my guess is...

Instead of Double.toString(onec)

Use String.valueOf(onec)

EDIT: If this is not the case, please elaborate on what your problem is, and a fuller code listing.

Stu Whyte
  • 758
  • 5
  • 19
  • I am using a separate method 'calone' which is inside a separate class 'one' to undertake some operation and storing the result in a variable 'onec' of double data type. – irfan Aug 01 '13 at 17:29
1

converting the contents of the textArea to double does not calculate sum. Try looping throught the first textArea reading each value while calculating the sum

james
  • 36
  • 1