GWT Drag&删除小部件

时间:2014-04-29 11:29:40

标签: java gwt drag-and-drop widget composite

我正在使用allen sauer的gwt dnd库来管理绝对面板中gwt小部件的简单拖动功能。

虽然这适用于简单的小部件(如图像),但我想使用自己的小部件(通过扩展合成)来完成。它根本不会拖拽和在我想要的时候删除自定义小部件。

下面是我的自定义窗口小部件的代码以及它们如何添加到绝对面板:

package GWTest.artid.client;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class DraggableWidget extends Composite {

    private Image image;

    private Label label = new Label();

    private Button button = new Button("Configure");

    private VerticalPanel panel = new VerticalPanel();

    public DraggableWidget(String imagePath, String labelText, boolean hasButton) {
        super();
        this.image = new Image(imagePath);
        this.label.setText(labelText);

        this.panel.add(image);
        this.panel.add(label);
        if (hasButton) {
            this.panel.add(button);
        }

        initWidget(panel);
    }

    public Image getImage() {
        return image;
    }

    public void setImage(String imagePath) {
        this.image = new Image(imagePath);
    }

    public Label getLabel() {
        return label;
    }

    public void setLabelText(String labelText) {
        this.label.setText(labelText);
    }

    public Button getButton() {
        return button;
    }

    public void setButtonText(String buttonText) {
        this.button.setText(buttonText);
    }
}

在onModuleLoad中(COMPUTER_WIDGET和ROUTER_WIDGET只是字符串,图像资源的路径):

absolutePanel.setPixelSize(600, 200);
dropController = new AbsolutePositionDropController(absolutePanel);
dragController = new PickupDragController(absolutePanel, true);
dragController.registerDropController(dropController);

DraggableWidget w = new DraggableWidget(DraggableFactory.COMPUTER_WIDGET, "Label 1", false);
DraggableWidget w1 = new DraggableWidget(DraggableFactory.ROUTER_WIDGET, "Label 2", true);

dragController.makeDraggable(w1);
dragController.makeDraggable(w);

dropController.drop(w1, 10, 30);
dropController.drop(w, 10, 30);

构建这些自定义小部件时是否有任何遗漏?

希望有更多经验的人可以帮助我...

1 个答案:

答案 0 :(得分:0)

我终于找到了这个:

public class MyWidget extends Composite implements HasAllMouseHandlers, HasClickHandlers {

...

      public HandlerRegistration addClickHandler(ClickHandler handler) {
        return addDomHandler(handler, ClickEvent.getType());
      }

      public HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
          return addDomHandler(handler, MouseDownEvent.getType());
      }

      public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
        return addDomHandler(handler, MouseMoveEvent.getType());
      }

      public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
        return addDomHandler(handler, MouseOutEvent.getType());
      }

      public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
        return addDomHandler(handler, MouseOverEvent.getType());
      }

      public HandlerRegistration addMouseUpHandler(MouseUpHandler handler) {
        return addDomHandler(handler, MouseUpEvent.getType());
      }

      public HandlerRegistration addMouseWheelHandler(MouseWheelHandler handler) {
        return addDomHandler(handler, MouseWheelEvent.getType());
      }

}

这个答案: Issue in making composite widget draggable 解决了我的问题,我可以使用任何类型的复合小部件与dnd现在