2

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,

Cameron Roberson
  • 111
  • 4
  • 20
  • this question doesn't make me sence – mKorbel Oct 09 '13 at 18:19
  • @mKorbel I am trying to have an actionevent occur when no action has taken place. For instance, you could contain an actionevent in a method and it would run even if there was no action that occured. I am trying to do this to put something into the EDT without an action occuring. – Cameron Roberson Oct 09 '13 at 19:27
  • then there are three ways, use Swing Action, PropertyChangeListener, EventHandler, seems like as (as far as understand) you need to read something about, search here Java + Swing + MVC or PropertyChangeListener, here are lots of Q&A (especially in posts by @Hovercraft Full Of Eels) really excelent code examples – mKorbel Oct 09 '13 at 19:35
  • @mKorbel The reason I wanted to do this is because I wanted to add text to a JTextPane, but it wouldnt update. I saw some of your other posts, and you reccomended using document filter. I will try using this to see what happens. Thanks – Cameron Roberson Oct 09 '13 at 19:53

2 Answers2

6

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();
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 1
    ... while being careful to realise that `doSomething()` when executed in isolation may not be running on the EDT. – Duncan Jones Oct 09 '13 at 15:31
  • @JB Nizet I think you need to click for this to occur correct? I want to have it occur with nothing happening and it running as if it were a method. thanks – Cameron Roberson Oct 09 '13 at 16:44
  • @Duncan Jones event in Swing Listener is done in the moment that all codelines from all invoked (its nested classes and methods too) are executed, in this case after all code from doSomething(); is executed – mKorbel Oct 09 '13 at 18:19
3

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.

Grodriguez
  • 21,501
  • 10
  • 63
  • 107
  • 1
    +1, I like invoking the ActionListener directly. Then all the code for the listener is contained in one place. If you invoke an external method you have code in two places. Swing was designed to be used with Actions. So the code should be self contained in the Action. – camickr Oct 09 '13 at 15:34
  • @Grodriguez Could you possibly provide a small example of what you mean with the listener.actionPerformed(new ActionEvent(source, id, "dummy")); – Cameron Roberson Oct 09 '13 at 15:48
  • @CameronRoberson: I am not sure what you're asking; the code in my answer is a small example where a dummy `ActionEvent` object is created. Can you please elaborate? – Grodriguez Oct 09 '13 at 16:55
  • @Grodriguez Sorry for the misscommunication. What I mean is could you provide a small example of how this would be implemented. I cannot visualize how this would work. What makes it behave like a dummy action event? What is the source, id, etc. Maybe provide a simple code example where it prints a line of text to output or something. Thanks for the persistence :) – Cameron Roberson Oct 09 '13 at 17:59
  • 1
    Normally an ActionListener is invoked when you click a JButton. Your requirement seems to be to invoke the ActionListener without the button. So you create the "listener" the same way you do for a JButton. `What is the source, id, etc.` - read the ActionEvent API if you don't understand what the parameters are. The source would be the button, the id would be the ActionPerformed event. The ActionEvent class contains a variable to represent this event. – camickr Oct 09 '13 at 20:00
  • @CameronRoberson What you put in source, id, etc. depends on what the `actionPerformed` method actually expects. Perhaps it is easier if you post a question describing exactly what you are trying to achieve (with sample code), then we can try to answer you in a more specific way. – Grodriguez Oct 10 '13 at 09:43