문제

I'm creating a composite panel which have ability to drag and drop files from system. It looks like bellow

public abstract class Upload extends Composite implements DragEnterHandler, DragLeaveHandler, DropHandler, DragOverHandler {
...

    @Override
    public void onDragOver(DragOverEvent event) {
        // TODO Auto-generated method stub
    }

and it doesn't work at all. But "almost" the same code

    initWidget(uiBinder.createAndBindUi(this));
    ...
    addDomHandler(new DragOverHandler() {
        @Override
        public void onDragOver(DragOverEvent event) {
            // TODO Auto-generated method stub
        }
    }, DragOverEvent.getType());

works pretty well.

The question:

Where is a difference? Here I read that there should be no difference. Is there still possibility to use it in "interface implements" way?

도움이 되었습니까?

해결책

Just because you implement the interface, doesn't mean that your implementation is used.

Let me give you an example:

public class FancyButton extends Button implements SomeFancyButtonListener
{
    public FancyButton()
    {
        // This line is necessary, otherwise the implemented code isn't used.
        this.addFancyButtonListener(this);
    }

    @Override
    public void fancyButtonClicked(FancyClickEvent e)
    {
        // Do something
    }
}

is equivalent to:

public class FancyButton extends Button
{
    public FancyButton()
    {
        this.addFancyButtonListener(new SomeFancyButtonListener()
        {
            @Override
            public void fancyButtonClicked(FancyClickEvent e)
            {
                // Do something
            }
        });
    }
}

From what I can see in your first code snippet, you aren't adding this as the Handler.

다른 팁

Handlers don't work until an unless it is added on the component as simple as it is.

In first case you have overridden the handler's method but you haven't added this handler on component itself but in second case you have added it on component along with its implementations that's why its working.


Handlers are just like listeners that works in same manner as Observer Pattern works.

If a component wants to listen for a specific event then it has to register for it. Whenever that event is triggered in the system it will be notified to all the registered components.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top