• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField ActionListener - user edits vs programmatic edits

 
alex earnshaw
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I have a JTextField which I have added an ActionListener to. Is there any way I can tell whether the call to actionPerfomed() has been triggered by the user entering text and pressing Return or by a call to setText() in my program?

I only want to do something when the user edits the field not when I change the text programmatically? Any suggestions greatly appreciated...
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
setText() doesn't fire an ActionEvent at all. Don't worry about it.
 
alex earnshaw
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok great, didn't realise.

I also have the same problem with JButton though - how to tell if the user clicks it or if I call doClick() in my code? Is there something in the ActionEvent that would tell me?

Thanks
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not aware of any way to tell the difference between a programmatic and manual click from the ActionListener (after all, a manual click is really just the LookAndFeel calling methods for programmatic click at the right time!). Assuming you only use doClick() from the event dispatch thread, you can just write a simple wrapper around doClick() that tracks a little extra information:



Then an action can call isFakeClick() to check if it was caused by a call to doFakeClick().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!