wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Is ActionListener and Key Listener related? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Is ActionListener and Key Listener related?" Watch "Is ActionListener and Key Listener related?" New topic
Author

Is ActionListener and Key Listener related?

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
I coded an application where there is 2 textfields having keylistener and a submit button with action listener.

For key and action listener, here is the coding
public void keyPressed(KeyEvent e) {



Its working fine. If i put any value in currentPaymenttext in press enter, its works. Now, (without pressing PAY button), again i change the value in currentPaymenttext and press enter, its updated with new value.

But, when i submit the form through PAY button(ActionListener), the code inside runs twice. On little bit of research i found that the number of times i change the value inside currentPaymenttext textfield in press enter, those many times submit code (i.e the code inside )runs, thus saving the same thing inside db those many number of times,

What is the mistake here?


kunal
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> On little bit of research i found that the number of times i change the value inside currentPaymenttext textfield in press enter, those many times submit code

sounds like each click of the button adds an extra actionListener.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks for your reply Michael. Yes, it adds additional ActionListener.

This was the previous coding


On removing subm.addActionListener(this);, subm.removeActionListener(this); from the if block, the problem is solved. But, i want that, if the dues >=0, then only sub button would work
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> i want that, if the dues >=0, then only sub button would work

are you sure?
your posted code adds the actionListener to subm if(dues >= 0) is true

have you thought about enable/disable subm based on if(dues >= 0)
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks Michael Dunn. It works much better and is easy to use
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is ActionListener and Key Listener related?