Interesting problem: Execution order of ActionListeners
My program is a test for students. When done, a student clicks on a button, and his/her answer is recorded and then sent via email to the instructor.
The button has two ActionListeners, the first is the answer recorder, and the second is the emailer.
The problem is that the emailer is executed before the answer recorder, before the answer is recorded. Is there a way to impose an order on the execution of these two ActionListeners?
Thanks!
[467 byte] By [
senore100a] at [2007-11-27 5:05:11]

# 1
This is an interesting question. I believe the order of notifying action listeners is the following: the sooner one was added, the sooner one will be informed. If I'm wrong about this, then it's the inverse. It seems to me that there are no ways to modify the order without removing action listeners and then adding them back in, as there are no priority fields that would communicate this. But this is just an educated guess.
# 2
Thanks for reply. You are right. It seems that the later an ActionListener is added, the earlier it is actually executed. I reversed the order of adding those two, and it is working.
# 3
> Thanks for reply. You are right. It seems that the
> later an ActionListener is added, the earlier it is
> actually executed. I reversed the order of adding
> those two, and it is working.
This may be the case but you should not rely on it. The order is undefined and in the next version of Swing the order may change.
# 4
Of course the real solution would be to have a single action listener that does two things:a) record the answerb) email the answer
# 5
Thanks. The reason I am not lumping two actions together is that the answer is recorded multiple times whereas email is sent only once. Conceptually isn't it also better to have two instead of one ActionListener? I am interested in finding out if there are any rules regarding the order.
# 6
> Thanks. The reason I am not lumping two actions
> together is that the answer is recorded multiple
> times whereas email is sent only once.
I don't see how this is a problem. You have a piece of code that does the recording, and another separate piece of code that does the emailing. How come you cannot call these two pieces of code from one single place in a deterministic order?
> Conceptually
> isn't it also better to have two instead of one
> ActionListener?
Not at all, especially not in your case where the two actions are coupled (through the order in which they must be invoked). It is most probably a good idea to separate the code that implements the recording from the code that implements the emailing, but I don't see any reason why it would be wrong to invoke these two pieces of code from one single place if you have to.
> I am interested in finding out if
> there are any rules regarding the order.
As have already been said, the order is undefined by the API so if you want a robust application you need to do what camickr suggested.
# 7
> Conceptually isn't it also better to have two
> instead of one ActionListener? I am interested in
> finding out if there are any rules regarding the order.
The reason it's possible to have more than one of a particular kind of listener is so different clients can use them for different purposes. For example, the Swing painting subsystem may use a ListSelectionListener on a JList to be notified when the JList needs to be repainted, while an application uses one in order to know when the user selects different data.
But when it comes to ActionListeners, I think it shouldn't even be possible to attach more than one to any given component. Actions are a higher-level abstraction, and I think only application programmers should be able to assign them. And to assign more than one action to a single event is usually a bad idea, as you've just discovered.
# 8
Thanks for all your explanation. Now I better see where the problem is. Yes, I can use just one instead of two ActionListeners so that there's no room for haphazard execution.