Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #2179 > unrolled thread

confused about the sequen

Started by"Ray Ma" <ray.ma@THRWHITE.remove-dii-this>
First post2011-04-27 15:37 +0000
Last post2011-04-27 15:37 +0000
Articles 4 — 4 participants

Back to article view | Back to comp.lang.java.gui


Contents

  confused about the sequen "Ray Ma" <ray.ma@THRWHITE.remove-dii-this> - 2011-04-27 15:37 +0000
    Re: confused about the se "Laird Nelson" <laird.nelson@THRWHITE.remove-dii-this> - 2011-04-27 15:37 +0000
    Re: confused about the se "Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this> - 2011-04-27 15:37 +0000
    Re: confused about the se "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:37 +0000

#2179 — confused about the sequen

From"Ray Ma" <ray.ma@THRWHITE.remove-dii-this>
Date2011-04-27 15:37 +0000
Subjectconfused about the sequen
Message-ID<1186005594.700709.257210@j4g2000prf.googlegroups.com>
  To: comp.lang.java.gui
Hi, all,

I have a question about execution order of event listeners when an
event occur and two or more listeners are registered for that event.
For example, I have a panel class that contains a textfield for enter
number and a "plus", a "minus" button for increase/decrease the
number. Inside this class, I add document listener for the textfield
changes.

class NumberPanel{

      JTextField numberTxFld = new JTextField();
      JButton upButton, downButton;
      int value;
      ...
      numberTxFld.getDocument.addDocumentListener(new
DocumentListener(){
                    ....
                public void insertUpdate(){
                      setValue();
                }
                public void removeUpdate(){
                      setValue();
                }
      });
     ...
    private void setValue(){
               this.value = Interger.parseInt(numberTxFld.getText());
    }
 }

 I also have another GUI component "CalculationPanel" that include
this numberPanel. The "CalculationPanel" needs to communicate with
other GUI component about the changes of the "value". So I also add
another document listener for the numberTxFld in "CalculationPanel". I
can't add this in the NumberPanel because the NumberPanel has no
reference/knowledge of the other GUI component.

  class CalculationPanel {

         NumberPanel numberPan  = new NumberPanel();
         JTextField  calculaterTxFld = numberPan.numberTxFld;
         OtherComponent otherComp = new OtherComponent();
         ......
         calculaterTxFld.getDocument().addDocumentListener(new
DocumentListener(){
                    ....
                public void insertUpdate(){
                      notifyOtherComponent();
                }

                public void removeUpdate(){
                      notifyOtherComponent();
                }
      });

     notifyOtherComponent(){
               otherComp.setNumberValue (calculaterTxFld.value);
     }
}

    In the Main() method, the CalculationPanel is added to the root
Frame. When I input to the textfield, I notice that the listener I
wrote in the "CalculationPanel" is executed first, then the listener
in "NumberPanel". I wonder why and how can I control their order of
execution?


Thanks

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#2181 — Re: confused about the se

From"Laird Nelson" <laird.nelson@THRWHITE.remove-dii-this>
Date2011-04-27 15:37 +0000
SubjectRe: confused about the se
Message-ID<1186014148.734289.69520@l70g2000hse.googlegroups.com>
In reply to#2179
  To: comp.lang.java.gui
On Aug 1, 5:59 pm, Ray Ma <Wenj...@gmail.com> wrote:
> I wonder why and how can I control their order of
> execution?

According to the Java beans specification, from which EventListeners
are derived, you can't.  An implementation other than Sun's is free to
fire listeners in any order whatsoever.

Pragmatically speaking, I know that a lot of implementations of event
broadcasting traverse the list in reverse order.  If you didn't care
about the specification, and only cared about this particular VM, you
could probably rely on that, as it hasn't changed in years.

Best,
Laird

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2190 — Re: confused about the se

From"Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this>
Date2011-04-27 15:37 +0000
SubjectRe: confused about the se
Message-ID<46b28be8$0$1600$ed2619ec@ptn-nntp-reader02.plus.net>
In reply to#2179
  To: comp.lang.java.gui
Ray Ma wrote:
> 
> I have a question about execution order of event listeners when an
> event occur and two or more listeners are registered for that event.
> For example, I have a panel class that contains a textfield for enter
> number and a "plus", a "minus" button for increase/decrease the
> number. Inside this class, I add document listener for the textfield
> changes.

You need to be really careful with state change listeners. In general, 
the state of the source may have been updated between the original 
update and the listener actually getting called.

To be safe, state change listeners should not depend upon the event 
object and should not update state (as such). As a rule, avoid 
duplicating state. Instead of having a 'value' field, have a 'getValue' 
method that reads the document to find the real value. It may even be 
more efficient, in significant measures.

If you really wanted to get into what has changed, either compare a copy 
or get deep into database-style transaction logs (neither I particularly 
recommend unless absolutely necessary).

Tom Hawtin

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2191 — Re: confused about the se

From"Roedy Green" <roedy.green@THRWHITE.remove-dii-this>
Date2011-04-27 15:37 +0000
SubjectRe: confused about the se
Message-ID<m0l5b3pvohfsikkrc20vl9vamcbp20gv9f@4ax.com>
In reply to#2179
  To: comp.lang.java.gui
>I have a question about execution order of event listeners

Sun makes no guarantees of the order. However,  if you look at the
code for JButton.addActionLister you will it is implemented in
AbstractButton.actionListener like this:
listenerList.add(ActionListener.class, l);

listener list is  an EventListenerList..

If you have a look at EventListenerList, it is implemented an array of
type/ listener pairs. Any time you add, it creates a new array, 2
slots  bigger and copies the old array over adding the type and
listener in the two new slots at the end.

You can see from  the getListeners code, it gets the elements in
ascending order.

Therefore the events are fired oldest-event-registered first in
practice.
-- 
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web