Draggable & Resizable Container in CodeName One
-
21-12-2019 - |
Question
I am trying to make my custom widget as draggable by setDraggable.
Part 1: My goal is to create a rectangular Container with title (North) having other components in it (Center). MyWidget should be dragged ONLY if I drag title.
public class MyWidget extends Container {
public MyWidget(String labelText) {
// Temp size. This will be removed after actual components
// are put into innerContainer
setPreferredH(100);
setPreferredW(400);
setLayout(new BorderLayout());
Label title = new Label(labelText);
addComponent(BorderLayout.NORTH, title);
Container innerContainer = new Container();
addComponent(BorderLayout.CENTER, innerContainer);
}
}
Container myWidget = new MyWidget("MyWidget");
myWidget.setDraggable(true);
setDraggable() works well when I used it for a Label, but I am not able to drag when I apply same method to MyWidget.
Part 2: Also, Plese suggest how can I make this widget resizable on long press. My goal is to give resizable indicator edges for container on long press & on press, widget enters resize mode. Then I resize component by dragging edges. Resize mode will end if I click outside MyWidget.
PS: I noticed dragging is happening 'sometimes' when I click at corner of widget.
Solution
You are trying to drag the individual component which is grabbing the drag event. I suggest trying something like:
setLeadComponent(title);
This will effectively convert the entire hierarchy into a single unified component that changes state together and works as one.
This:
setPreferredH(100);
setPreferredW(400);
Is a mistake, you shouldn't use setPreferred normally however if you do so you should always use:
setPreferredSize(new Dimension(400, 100));