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 Manually sending events? 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 » Java » Swing / AWT / SWT
Bookmark "Manually sending events?" Watch "Manually sending events?" New topic
Author

Manually sending events?

Gulshan Singh
Ranch Hand

Joined: Apr 22, 2009
Posts: 41
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

Joined: Apr 07, 2007
Posts: 230
Gulshan,

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

Jeff


Jeff Storey
Software Developer
[url]http://jeffastorey.blogspot.com[/url]
Gulshan Singh
Ranch Hand

Joined: Apr 22, 2009
Posts: 41
Oh, I didn't know that. Thanks!
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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

Joined: Oct 13, 2005
Posts: 38041
    
  22
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

Joined: Sep 20, 2005
Posts: 862
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.

bitguru blog
Gulshan Singh
Ranch Hand

Joined: Apr 22, 2009
Posts: 41
Ok, I actually might do that instead. Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38041
    
  22
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

Joined: Sep 20, 2005
Posts: 862
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]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Manually sending events?
 
Similar Threads
h:commandButton + actionListener
How to open a separate new window
how to put interest rates in my applet
howto detect which button is click
simple JOptionPane help