Forums Register Login

How to fire an Action[urgent]

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
This will take every ounce of my mental strength! All for a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1651 times.
Similar Threads
Can't find constructor!
ActionListener illegal start of expression
Component.setEnabled()
Two ActionListener
getting user input
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:34:12.