LongEvent with a multibutton

60 views
Skip to first unread message

State Thirteen

unread,
Nov 6, 2013, 5:16:51 AM11/6/13
to codenameone...@googlegroups.com
Hi,

I am trying to make the Multibutton change it's Draggable property when LongEvent occurs. 
Basically I have a container with buttons and I want to arrange them by dragging and dropping into the new position.
I think that long tap should make the button draggable for this. So I am trying to do like this

mb.setCommand(new Command("") {
public void actionPerformed(ActionEvent ev) {
if (ev.isLongEvent()){
System.out.println("LongEvent");
ev.getComponent().setDraggable(true);
}else{
showTagsForm(f);
}
}
});

But I didn't get anything on the emulator.

Shai Almog

unread,
Nov 6, 2013, 1:15:32 PM11/6/13
to codenameone...@googlegroups.com
Hi,
this is a bit confusing since we use action events for many things but in this case we won't distinguish a long press from a regular press. The only way you can do this is by binding a pointer listener to the form and detecting a long press there.
You can then find the component at x/y and if its a MultiButton you can toggle its draggability.

State Thirteen

unread,
Nov 6, 2013, 2:01:01 PM11/6/13
to codenameone...@googlegroups.com
I have found longPointerPress(). Now the button is changing to draggable on long press. But I can't drag it in while pointer is still pressed. 
I have to release the pointer and then I can drag the button. How can I drag it instantly?

final MultiButton mb = new MultiButton(){
@Override
public void longPointerPress(int x, int y){
System.out.println("LongPress");
this.setDraggable(true);
}
};


State Thirteen

unread,
Nov 6, 2013, 3:31:32 PM11/6/13
to codenameone...@googlegroups.com
Farther question. I am trying to save buttons order after drag&drop operation. But I am getting the order which was before drag&drop.
Should I use any other listener or how can I get the new buttons order in the container?

mb.addDropListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {
System.out.println("Dropped");
evt.getDraggedComponent().setDraggable(false);
saveList(f,"Communities");
}
});

private void saveList(Form f, String name) {
//f.revalidate();
Vector<String> vec = new Vector<String>();
int num=findMainContainer(f).getComponentCount();
MultiButton mb;
for (int i=0;i<num;i++){
mb=(MultiButton)findMainContainer(f).getComponentAt(i);
vec.add(mb.getTextLine2());
}
System.out.println(vec);
Storage.getInstance().writeObject(name, vec);
}


Shai Almog

unread,
Nov 7, 2013, 12:55:09 AM11/7/13
to codenameone...@googlegroups.com
This is a bit tricky.  You can fake pointer release by invoking the forms pointer released/pointer pressed/pointer dragged methods to essentially create that situation in code.

I'm guessing you are higher in the event chain when compared to the actual drop callback that rearranges the elements and that event hasn't yet happened. The trick is to push your code to the back of the event chain by wrapping that line in a callSerially() which will deffer it to the next event loop.

State Thirteen

unread,
Nov 7, 2013, 2:44:50 AM11/7/13
to codenameone...@googlegroups.com
Shai,

Thank you. I'll try to do this trick )
Reply all
Reply to author
Forward
0 new messages