This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes actionlistener repeats Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "actionlistener repeats" Watch "actionlistener repeats" New topic
Author

actionlistener repeats

Gary Down
Greenhorn

Joined: Jan 04, 2008
Posts: 11
I have an action listener attached to a NumericTextField (extends JTextField).
I display 3 of these then enter 0 which is detected by the listener.
No problems so far.
The first time the listener is fired 1 time.
the 2nd time the listener is fired 2 times.
the 3rd time the listener is fired 4 times.

What is causing the repetitions and how do I prevent it.
AM I right in thinking it has something to with using a Timer?

Thanks,
Gary.



output is :-
java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=0,when=1200452253421,modifiers=] on AA AA
deleted true
java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=0,when=1200452265827,modifiers=] on BB BB
deleted true
java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=0,when=1200452265827,modifiers=] on BB BB
deleted false
java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=0,when=1200452270327,modifiers=] on CC CC
deleted true
java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=0,when=1200452270327,modifiers=] on CC CC
deleted false
java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=0,when=1200452270327,modifiers=] on CC CC
deleted false
java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=0,when=1200452270327,modifiers=] on CC CC
deleted false
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Hi,

I'm throwing a quick idea..

Could it be that the code listening to the action also calls addActionListener, so the listener actually gets added once more every time ?

Can you try inserting a System.out where you do your "addActionListener" and see if its called more than once.

Regards,
Alex
Gary Down
Greenhorn

Joined: Jan 04, 2008
Posts: 11
I believe you're right.

The culprit (I think) is itemCount in the displayCart which has an actionlister added each time it is added to the display (a JPanel) is called to re create the display after an item is deleted. Now to figure a better way to do it.

Thanks,
Gary.
javascript: x()
jumpingjoy

Gary Down
Greenhorn

Joined: Jan 04, 2008
Posts: 11
sorry.
this.add(cartItem.getItemCount());

should read
this.add(itemCount);

was moving things around a little....
Gary Down
Greenhorn

Joined: Jan 04, 2008
Posts: 11
I believe that was a red herring, no matter what I try I can't seem to find the problem (doesn't mean there isn't one).

Just to restate the problem.
The first time I enter 0 (zero) into one of the NumericTextFields all seems OK, the entry is deleted and the Jpanel redisplayed with the now deleted item erased.
Second and subsequent times I enter ), the field is deleted correctly but fires multiple action events which are detected by the ActionListener. It works but seems a lot of excessive processing.
If I delete all the fields (by entering 0 in one after the other) the Jpanel is not changed after the last delete (i.e. the last is still showing).
Gary Down
Greenhorn

Joined: Jan 04, 2008
Posts: 11
Thanks me.
solved the first part. I was adding the actionlistener to the NumericTextField object in the list everytime the display function was called. Moved the add to where the object is created and added to the list.

Still haven't figured the last delete bit yet.

Any comment/advise welcome..

Gary.

Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Hi,

not sure what you mean by "not figured the delete part yet".

If you are referring to your 1st post, where some of your output showed "delete false", it should be because after the first call (listener), the object is already removed, so it returns false.

Otherwise, could you please provide some more details on your issue.

Regards,
Alex
[ January 16, 2008: Message edited by: Alex Belisle Turcot ]
Gary Down
Greenhorn

Joined: Jan 04, 2008
Posts: 11
Thanks Alex,
you first reply got me headed in the right direction to fix the repeated actionlistener events.

The other bit I was referring to was from a later comment I posted
"If I delete all the fields (by entering 0 in one after the other) the Jpanel is not changed after the last delete (i.e. the last is still showing)."

I think it has something to do with the validate() statement when nothing has been added to the panel when there is nothing left in the set to display. I'll try a few things around that tomorrow.

Thanks again,
Gary.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: actionlistener repeats
 
Similar Threads
Map Problem
Confused for threading ?
Member object talks back to parent object
static methods and NULL
Operators and Precedence