Member Avatar

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.

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?

Member Avatar

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.)

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?

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?

Member Avatar

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.)

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?

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.

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

Member Avatar

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.)

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.

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.

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

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 :-)

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) {
   public void removeChangeListener(ChangeListener listener) {
   void notifyListeners() {
      // call this whenever data is changed
      ListIterator<ChangeListener> iterator = listeners.listIterator();
      while (iterator.hasNext()) { ChangeEvent(this));
commented: heheheh good one :-) +9

ComponentListener + Model + javax.swing.Action

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

Member Avatar

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!

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