On 29 sep, 15:27, YoeZ <[EMAIL PROTECTED]> wrote:
> Thank you very much, thomas
> here's the final sample code and it really works... :)
>
> but the problem is, in the MainMenu, I don't know which listbox that
> send the listener.

As I said, a Composite is meant to encapsulate (hide) the internals of
your widget.

> in the MainMenu, I provide three textboxes
> txtListA = catch the event from listA
> txtListB = catch the event from listB
> txtListC = catch the event from listC
> so, I have to know first, the name of the sender widget, (which is
> listA? listB? or listC?)
> then set the txtListA.setText(listA); and so on

Then either:
 - don't implement SourcesChangeEvents but have 3 add* and 3 remove*
methods (one pair for each list). Each pair of methods will "drive"
its own ClickListenerCollection (hint: instead of implementing
ClickListener to fireChange on the collection, use a
DelegatingClickListenerCollection which does the job for you; see
below my rewrite of your ComboParent)
 - use another kind of event where you can pass a "name" for the thing
that has changed (PropertyChangeEvent for example)
 - somehow "abuse" the sender of the events (i.e. you register an
event on a widget but when it fires an event and call back your
listener, it isn't itself the "sender").

> I have no idea, how to get the sender name.
> I have tried sender.getclass().getname() but the result is not what I
> want.

The argument to fireChange will be the "sender" passed to the
listeners. In the code I gave you, it was "this", i.e. the Composite
(see above, third choice, if you intend to change the value of this
argument).

> /****************
> * iCombo.java
> ****************/
>         ListA.addChangeListener(new ChangeListener() {
>             public void onChange(Widget arg0) {
>                 listeners.fireChange(ListA);
>             }
>         });
>         ListB.addChangeListener(new ChangeListener() {
>             public void onChange(Widget arg0) {
>                 listeners.fireChange(ListB);
>             }
>         });
>         ListC.addChangeListener(new ChangeListener() {
>             public void onChange(Widget arg0) {
>                 listeners.fireChange(ListC);
>             }
>         });

OK so you chose to "abuse" the "sender"...
So how about setting a name to your lists (ListA.setName(...)) and
calling ((ListBox)sender).getName() in your listener? Kind of hackish
but "abusing" the sender is not better so...

> /****************
> * iComboParent.java
> ****************/
> public class iComboParent extends Composite implements
> SourcesChangeEvents  {
>     private HorizontalPanel vPanel = new HorizontalPanel();
>     private iCombo MyCombo = new iCombo();
>     private Button btn1 = new Button("OK");

    private DelegatingChangeListenerCollection listeners = new
DelegatingChangeListenerCollection(this, MyCombo);

// Please note that you loose your "abuse the sender" approach using a
DelegatingChangeListenerCollection

>     public iComboParent() {
>         vPanel.add(MyCombo);
>         vPanel.add(btn1);
>
>         initWidget(vPanel);

And just get rid of the following, which is already done by the
DelegatingChangeListenerCollection in its constructor:

>         MyCombo.addChangeListener(new ChangeListener() {
>             public void onChange(Widget sender) {
>                 listeners.fireChange(sender);
>             }
>         });

> /****************
> * MainMenu.java
> ****************/
>         cp.addChangeListener(new ChangeListener() {
>             public void onChange(Widget sender) {
>                 ListBox lb = (ListBox) sender;
>
> txtListA.setText(lb.getItemText(lb.getSelectedIndex()));
>             }
>         });
>
>         RootPanel.get().add(vPanel);
>     }

If I were you, I'd really either
 - create a new kind of listener communicating the changed "list name"
and selected value;
 - expose the ListBoxes with getters (bad looking but I don't really
understand your layout, so...)
 - use 3 pairs of add/removeChangeListener so by registering a
ChangeListener you know where to put the value; and expose the
selected value with a getter:
   cp.addListAChangeListener(new ChangeListener() {
      public void onChange(Widget sender) {
         txtlistA.setText(cp.getSelectedValueA());
      }
   });

Anyway, your arrangement looks weird (to me)...
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to