2014-11-20 63 views
0

我正在创建一个下拉列表,它应该为表中的特定行创建,删除和提交。有人可以帮助我如何在GWT中创建一个。如何在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); 

回答

0

首先,不要在onCellPreview方法中构建你的菜单。每次点击都不需要重复构建相同的小部件。

你可以做这样的事情:

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 
    } 
}