How to create a drop down list on right click in GWT

508 views Asked by At

I am creating a Dropdown list which should have CREATE, DELETE, SUBMIT for a particular row in a table. Can someone help me how to create one in GWT.

   table.addCellPreviewHandler(new Handler<RequestDto>()
    {

        @Override
        public void onCellPreview(CellPreviewEvent<RequestDto> event)
        {
            if (event.getNativeEvent().getButton() == NativeEvent.BUTTON_RIGHT)
            {


                MenuBar options = new MenuBar();
                MenuBar gwtPop = new MenuBar();
                options.addItem("Create", gwtPop);
                options.addItem("Submit", gwtPop);
                MenuItem Import = new MenuItem(new SafeHtmlBuilder().appendEscaped("Import").toSafeHtml());
                Import.setScheduledCommand(new ScheduledCommand()
                {

                    @Override
                    public void execute()
                    {
                        Window.alert("hello");
                    }
                });
            final DialogBox menuWrapper = new DialogBox(true);
                menuWrapper.add(options);
                gwtPop.addItem(Import);
1

There are 1 answers

0
Andrei Volgin On BEST ANSWER

First of all, do not construct your menu inside onCellPreview method. There is no need to build the same widget over and over again on each click.

You can do something like this:

int clickedRow = -1;
...
// build your menu here
// use clickedRow where necessary, for example:

deleteMenuItem.setScheduledCommand(new ScheduledCommand() {

    @Override
    public void execute() {
        Window.alert("hello, I am about to delete row " + clickedRow);
    }
});
...
myTable.addCellPreviewHandler(new Handler<MyObject>() {

    @Override
    public void onCellPreview(CellPreviewEvent<MyObject> event) {
        if (event.getNativeEvent().getButton() == NativeEvent.BUTTON_RIGHT) {
            event.getNativeEvent().stopPropagation();
            clickedRow = event.getIndex();
            // show your menu - no need to construct it again 
    }
}