Cameron Roberson
Cameron Roberson

Reputation: 111

Can you call an action event in java without an action

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

Answers (2)

Grodriguez
Grodriguez

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

JB Nizet
JB Nizet

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

Related Questions