• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is ActionListener and Key Listener related?

 
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Michael Dunn. It works much better and is easy to use
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic