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 / JFace and the fly likes How to fire an Action[urgent] Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT / JFace
Reply Bookmark "How to fire an Action[urgent]" Watch "How to fire an Action[urgent]" New topic
Author

How to fire an Action[urgent]

netharam ram
Ranch Hand

Joined: Aug 09, 2001
Posts: 202
Hi, I want the actionlistener of a JButton for Eg.
to be fired without clicking the Button, but from some other source.Please suggest something different rather than defining a method & calling it in the actionListener().
Happy middling with java
Netharam
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
I'm confused when you say "Please suggest something different rather than defining a method & calling it in the actionListener()."
This seems like the best approach.
I.e, encapuslate the functionality you want to perform in a seperate method. Then call that method from within your actionPerformed() method in response to a button click. Then, you can also call your function method from some other source, and get the exact same behavior as when the user clicks the button.
You can also look at the javax.swing.Action interface. This provides an interface to encapsulate an action that can be invoked by several different components.
Rob


Rob
SCJP 1.4
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Netharam,
Well you could go ahead and add a new action into the event queue as follows:
public void fakeAction(Component c) {
getToolkit().getSystemEventQueue().postEvent(
new ActionEvent(c, ActionEvent.ACTION_PERFORMED, ""));
}
That should make it look like someone pushed the button. The difference between this and what Rob mentioned is that in this case all button listeners will get notified that the button was pressed.
Regards,
Manfred.
[ January 26, 2002: Message edited by: Manfred Leonhardt ]
 
IntelliJ Java IDE
 
subject: How to fire an Action[urgent]
 
Threads others viewed
Component.setEnabled()
getting user input
ActionListener illegal start of expression
Can't find constructor!
Two ActionListener
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture