• 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

JButton clicked once, action done twice

 
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings all,

Initially, I am not supplying code because I do not think it will help too much, but if anyone wants to see a bit, I can provide it.

In short, I have a dialog box with a bunch of fields and a save button. the save button implements an action listener that calls the save(). It reads the fields, creates an instance of another class and calls the methods to have it saved to my database. The problem is, that when I click once on button, the save() is called twice. The only thing I can think of is that I should be using a mouseAdapter or mouseListener instead of an actionListener. Can anyone confirm this?


Thanks,
Greg
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check your assumptions. You may have added the listener twice.

Inside the listener's actionPerformed(... ae) add this line:1 or 2?
 
Greg Reeder
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're correct, there are two.

Thanks, that is a handy line o' code there.

Greg
 
Greg Reeder
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've checked my code again (with help of the outline), and I only add one action listener to the JButton, I am really confused at the moment how it could be 2, other than the (really likely) possiblitly that the JButton already has one when instantiated.
 
Greg Reeder
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
upon further testing, that codes sometimes reads that it has 1 actionListener, two, I even got 5 one time. I am highly interested as to the reason why that is. . . . I will prepare code to post tomorrow.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm almost willing to bet that you'll find the problem yourself while preparing that code.

Specifics to watch out for: adding a listener within a listener method or in code called from that method; adding a listener within a painting method override (again, or in code called from a painting method override).
 
Greg Reeder
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You were absolutely right, although the problem was a little more hidden than I originally thought. This issue was that I have a private method that prepares the JButtons and JPanel that houses them. this method had the ability to be called multiple times depending one what the user did, and upon other factors as well. That meant that more than one action listener was added causing the multiple saves.

Thanks a lot.

Greg

If it helps others, I solved the issue by adding a simple question if (JBtn.getActionListeners().length<1)){//add the listeners}.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic