Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

actionlistener repeats

 
Gary Down
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry.
this.add(cartItem.getItemCount());

should read
this.add(itemCount);

was moving things around a little....
 
Gary Down
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic