File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JButton clicked once, action done twice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JButton clicked once, action done twice" Watch "JButton clicked once, action done twice" New topic
Author

JButton clicked once, action done twice

Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

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

Joined: May 03, 2008
Posts: 4542
    
    5

Check your assumptions. You may have added the listener twice.

Inside the listener's actionPerformed(... ae) add this line:1 or 2?


luck, db
There are no new questions, but there may be new answers.
Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

You're correct, there are two.

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

Greg
Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

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

Joined: Jun 14, 2011
Posts: 99

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

Joined: May 03, 2008
Posts: 4542
    
    5

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

Joined: Jun 14, 2011
Posts: 99

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}.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JButton clicked once, action done twice