I Have this:
public void actionPerformed1(ActionEvent e) { ... }
public void actionPerformed2(ActionEvent e) { ... }
public void actionPerformed3(ActionEvent e) { ... }
JButton b = new JButton();
b.addActionListener(this::actionPerformed1);
b.addActionListener(this::actionPerformed2);
b.addActionListener(this::actionPerformed3);
The code always executes action 3, then 2 and 1 in that order.
Is it possible to stop the execution of the queue without throwing a run time exception?
Thanks for your time.
I think you cannot stop it without using an Exception. If you look at the code in JButton which gets executed when its firing an ActionEvent (s.b.), you see that its just iterating in a for loop over a copy of the array of registered ActionListeners.
So you can try within the callback methods to unregister all Actionlisteners but this does not help since the loop uses a local copy. And you cannot increment the variable i from the callback neither.
protected void fireActionPerformed(ActionEvent e)
{
// Dispatch a copy of the given ActionEvent in order to
// set the source and action command correctly.
ActionEvent ae = new ActionEvent(
this,
e.getID(),
getActionCommand(),
e.getWhen(),
e.getModifiers());
ActionListener[] listeners = getActionListeners();
for (int i = 0; i < listeners.length; i++) //no way to prematurely terminate this loop
listeners[i].actionPerformed(ae);
}
However you can use a common variable in your class to signal in your callback methods, that they should just return, but this would not avoid their execution, but rather execute code which just decides to prematurely return.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With