Reputation: 111
I was wondering if you can have an action event in java occur when no action has taken place. What I mean is tricking it into thinking an action has occured when it has not and then running an action event. The reason I ask this is because it would seem like an easy run around to update things that run on the event dispatch thread without an actual event to occur. Let me know if any of you have heard about this. Thanks,
Upvotes: 2
Views: 7075
Reputation: 21995
The ActionListener.actionPerformed()
method expects an ActionEvent
argument, but you can call it directly and pass a dummy ActionEvent
object, such as:
listener.actionPerformed(new ActionEvent(source, id, "dummy"));
Whether this makes sense or not depends of course on the actual implementation of the actionPerformed()
method.
Upvotes: 3
Reputation: 691943
Well, you just need to delegate to a method:
// event listener for the click of the button:
public void actionPerformed(ActionEvent e) {
doSomething();
}
// other code wanting to do "as if the button was clicked":
doSomething();
Upvotes: 6