aspose file tools
The moose likes Swing / AWT / SWT and the fly likes Is ActionListener and Key Listener related? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply 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: 609
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: 609
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: 609
Thanks Michael Dunn. It works much better and is easy to use
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Is ActionListener and Key Listener related?
 
Similar Threads
How to write an event for "Enter" key?
Which JButton is being pressed ? OK or Cancel ?
How to generate Action on JComboBox click
button action command from keylistener
How to set focus on submit button on press of enter key