954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

ActionListener Firing Order

I've created a component (a replication of a JSpinner) with a JTextfield, and two JButtons.
I've created ActionListeners on each of the JButtons to update the JTextfield.

In my main program, I've create new objects based on my new component.
I've added ActionListeners on my new objects.JButtons.
My new ActionListeners are firing BEFORE my component's ActionListeners.
Is there a way to REVERSE the firing order?
ie. The component's ActionListeners to fire BEFORE my object's ActionListeners.

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 

Action listeners are put in a list and, I assume, are called based on their order in the list.
Can you put them in the list (ie add them) in the order you want them called?
Does the component have methods for getting to the list of listeners that would allow you to change their order?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Understood.
The question now becomes, how do I add the ActionListeners of the component to the execution list BEFORE I add the ActionListeners of my objects?
(Your questions, NormR1, are actually MY questions.)

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 
how do I add the ActionListeners of the component to the execution list BEFORE I add the ActionListeners of my objects?


The same way do you do anything in a program. You execute the statements in order that you want them executed.
I'm not understanding your problem. If you want A done before B, the do A first and then do B.

Is there one object with a number of actionListeners?
When the object has an action, does it call the listeners that have been added to it in the same order as they were added?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

What is the purpose of the new action listeners you are adding to the component? Are they to track when the value of the field is being changed by the buttons?
If so, wouldn't it be better to implement an addChangeListener method for the whole component that will register listeners and call them back passing a ChangeEvent after updating the text field?

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

The component already has an ActionListener (on each JButton) to update the JTextfield (and variable).
I have added NEW ActionListener(s) in my program to update another JTextfield in MY program based on the variable in the component.
I do have a "work around", but I would rather get the ActionListener(s) to fire in the desired order.
(The ActionListener in the component FIRST, THEN the ActionListener in MY program SECOND.)

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 
I would rather get the ActionListener(s) to fire in the desired order. (The ActionListener in the component FIRST, THEN the ActionListener in MY program SECOND.)


Can you add the component's AL first and the MY program's AL second?

Did you look at the methods for adding, gettting and removing action listeners?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

That's what I suspected. Just take a pace backwards and look at this from a higher level. You don't want to know about the individual buttons firing - that's just part of the internal implementation of your new component. You want to know when the value of the variable in the component has changed, and don't care if that was from a button, an up or down arrow key, mouse wheel scroll, or any other user interface you may implement now or later. You just want a ChangeListener on the component.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

that reason why javax.swing.Action exists, then you can forgot for override KeyBindings, PropertyChangeListener and MouseListener,

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

I tried adding a ChangeListener to my ("new" copy of) component.
"cannot find symbol" pointing to the .addChangeListener!
(Note: I don't have a ChangeListener in the component, if THAT makes any difference.)

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 

You would need to add that method to your class if your class does NOT extend a class that has it. Look in the API doc for a long list of classes that have that method.
You would also need the remove method and an internal list to save listeners in and a method to call the listeners when there is an event.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Yes, Norm's right. It's about a dozen lines of code in total; nothing difficult. Depending on you application you may not need the remove method, and you may be able to work with just a single listener instead of a list - that will almost halve the code, and you can always upgrade them later without changing any public method signatures.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Or... if your class doesn't extend anything yet you can simply extend Observable and inherit all the methods you need.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

OP's probably coding for Alliens, and in connections with these fact he's prohibited to sent here more infos, all of us have got false impression, maybe then there is for OP's more than fun :-)

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

OK hfx.. I've just been made "member of the month", so I'm in a really really good mood. Here's some code as a free gift from me (non-commercial use only).

// ChangeListener implementation
   
   ArrayList<ChangeListener> listeners = new ArrayList<>();
 
   public void addChangeListener(ChangeListener listener) {
      listeners.add(listener);
   }
   
   public void removeChangeListener(ChangeListener listener) {
      listeners.remove(listener);
   }
   
   void notifyListeners() {
      // call this whenever data is changed
      ListIterator<ChangeListener> iterator = listeners.listIterator();
      while (iterator.hasNext()) {
         iterator.next().stateChanged(new ChangeEvent(this));
      }
   }
JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

ComponentListener + Model + javax.swing.Action

some listeners allows Listener#Method#putProperty(String, String)

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

That all seems like WAY too much work.
I'll just implement my "work around".
Since I know which direction (Up button or Down buttn),
I'll read the value of the component's variable and,
testing limits, increment or decrement the programs variable BEFORE
the component's variable is reset.
Thanks anyways guys and gals!

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 

That's a shame! I always hate to see programs kludged like that. Never mind, it's your project, not mine. If you change your mind let me know and I'll walk you thru the tiny amount of code needed to do this "properly". Good luck. J

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You