This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Manually sending events?

 
Gulshan Singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I have a button and I want to have a function that will send a message to the computer saying that button is clicked even though it wasn't clicked. For example, there is a Yes button and a Cancel button. After 10 seconds the Yes button is automatically selected. I don't want to copy what the Yes button does when 10 seconds are up, I want to actually send a command to the actionlistener saying the button was clicked. This may seem strange, but I need to know if there's a way to do it...
 
Jeff Storey
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gulshan,

You can programmatically click a button by calling its doClick method.

Jeff
 
Gulshan Singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I didn't know that. Thanks!
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was gonna say, have your event handler consist only of a method call, then you can manually call the same method. But I didn't know about doClick(). Cool! might use that myself one day.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There might also be methods which fire a particular ActionEvent, but it is a long time since I looked for that sort of thing and I might not have remembered it correctly.
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gulshan Singh wrote:I want to actually send a command to the actionlistener saying the button was clicked. This may seem strange, but I need to know if there's a way to do it...

I concur on the doClick(), but do note that it will cause the button will actually appear pressed momentarily in the GUI. Contrast this with the button's fireActionPerformed() method.

But there's nothing wrong with calling your ActionListener's actionPerformed() method directly. In fact, I have sometimes called myListener.actionPerformed(null) when I knew (because I wrote it) that the listener completely ignored the event. This is fine.

There are things to keep in mind if you didn't write or have control over the listener. One is that sometimes an actionPerformed() method will assume (reasonably so) that it is called only on the EDT, so unless you know otherwise be sure to call it only from the EDT.
 
Gulshan Singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I actually might do that instead. Thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Cole wrote: . . . But there's nothing wrong with calling your ActionListener's actionPerformed() method directly. In fact, I have sometimes called myListener.actionPerformed(null) when I knew (because I wrote it) that the listener completely ignored the event. . . .
Your posts always have something new and useful in. I presume you use the getListeners() method or similar to find the Listener.

That is another argument against using the addActionListener(this) idiom. Imagine trying a .getSource() call on a null reference
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I presume you use the getListeners() method or similar to find the Listener.

Well I had a local reference to the listener, so I just called it directly. It wasn't much different from calling any other method not named actionPerformed on that object. (In fact, it would probably have been cleaner to move the implementation into a new zero-arg method and have actionPerformed simply call that method.)

If you must track down the listener with something like getListeners() then it's easier to just call fireActionPerformed(event) or doClick().

[edit: added parenthetical sentence]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic