tags:

views:

748

answers:

1

I'm having trouble adding a variable amount of labels to a panel. My problem is that for some reason when adding my clicklistener it returns void instead of widget (error). If I just have "new Label('xyz')" that works fine, but I need each panel to have their own clicklistener as well. Here is the code:

for(int x = 0;x<productIDArray.length();x++) {
  mainPanel.add(new Label("Test").addClickListener(new ClickListener() {
    @Override
    public void onClick(Widget sender) {
    // TODO Auto-generated method stub
     }
  }));
 }
+3  A: 

The returned value from method chaining is always the last value. If you change new A().b().c(), the returned type will be c's return type.

In your example, the return type of new Label("Test").addClickListener(... is the ClickListener's addClickListener return type, which is void.

You can create the label, add the click listener and then add it:

for(int x = 0;x<productIDArray.length();x++) {
  Label l = new Label("Test);
  l.addClickListener(...);
  mainPanel.add(l);
 }
Miguel Ping
Thanks, I was worried that it would just continue to add the same label or throw an error because of the loop but I see that is not the case.
Organiccat