Agostino Leoni
Agostino Leoni

Reputation: 3

gwt widget that contain another widget: 2 different clickhandlers

I'm studying the GWT framework and I'm trying to create a custom widget: this widget is a button that contain inside a menu of operations.

if you click in the area of the triangle I want a menu with some options (that are possible operations) and if I click in the other parts of the buttons I want that the operation is the first from the list. I have put a ListBox widget inside a Button widget and I want 2 different clickListener. The problem is that the listener of the listBox inside the button don't work. Do you know why?

Following the code

public class MyClass extends Composite {

private ListBox options;
private Button saveButton;
private HorizontalPanel savePanel;
private int indexHandler;

public MyClass(String label, List<String> operationList, final List<Command> commandList) {

    savePanel = new HorizontalPanel();
    initWidget(savePanel);

    options = new ListBox();
    saveButton = new Button(label);


    for(String operation : operationList){
        options.addItem(operation);
    }

    options.sinkEvents(Event.ONCLICK);

    options.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {

            System.out.println("Test1");
            indexHandler = options.getSelectedIndex();
            commandList.get(indexHandler).execute();
            options.setItemSelected(0, true);
        }
    });

    saveButton.getElement().appendChild(options.getElement());

    saveButton.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {

            System.out.println("Test2");

            commandList.get(0).execute();
            options.setItemSelected(0, true);

        } 

    });

    savePanel.add(saveButton);
}

}

Upvotes: 0

Views: 152

Answers (1)

Andrei Volgin
Andrei Volgin

Reputation: 41089

Don't use ClickHandler on the ListBox. Use ChangeHandler instead.

Also, I don't think you need to mess with Elements here. Simply add your Button widget and your ListBox widget to a container (i.e. some panel). You can add button on top of ListBox, if you want.

Upvotes: 2

Related Questions