• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JButton clicked once, action done twice

 
Greg Reeder
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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: 5125
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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}.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic