Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to fire an Action[urgent]  RSS feed

 
netharam ram
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!