Tejas jain
Tejas jain

Reputation: 752

Drag and Drop in CodeName One

I am trying to build an application in which I need to select widget from menu & drop it on Container (Not CUT & Paste, But select widget & Create its corresponding instance on other Container) in Drag & drop way.

I am not able find any help on this. Please help.

So far, I have done this,

Label label = new Label("Top Left");
label.setPreferredW(200);
label.setPreferredH(30);
label.getUnselectedStyle().setBgColor(0xff0000);
label.setDraggable(true);
stateMachine.findForm().addComponent(label);

// Setting parent can be recieve drop
stateMachine.findElementContainer().setDropTarget(true);

Container c = new MyContainer();
stateMachine.findElementContainer().addComponent(c);

public class MyContainer extends Container {

    public MyContainer() {
        this.setDropTarget(true);
    } 

    @Override
    public void drop(Component dragged, int x, int y) {
        System.err.println(dragged + " : " + x + " : " + y);
    }

}

But Method,

public void drop(Component dragged, int x, int y) {

is not being invoked.

Upvotes: 1

Views: 647

Answers (1)

Shai Almog
Shai Almog

Reputation: 52760

You set both the ElementContainer and its child as drop targets. You add c into the Elements container but since its a FlowLayout (the default) it won't size much and I'm assuming that ElementContainer doesn't have the right size of layout to give it the proper size.

Set the layout of ElementContainer to BorderLayout place c in the center and don't make the ElementContainer a drop target too. You should look at the how do I videos specifically the one dealing with layout managers. Setting the preferred size isn't the right direction.

Upvotes: 1

Related Questions