I have been following this guide and have the working example till the very end of the demo (very insightful tutorial, 10/10 would recommend):
Now I wanted to expand this to a more practical todo list by adding a Floating Action Button to add new items, like that:
fab = FloatingActionButton.createFAB(FontImage.MATERIAL_ADD);
fab.bindFabToContainer(current.getContentPane(), Component.RIGHT,
Component.BOTTOM);
fab.addActionListener(e -> { // show dialog for adding new item });
This by itself works fine. Now comes the tricky part. When using a long press event on any of the items, two things should happen:
The long press event is achieved in overriding the longPointPress method from the Checkbox class:
@Override
public void longPointerPress(int x, int y){
mainForm.longPressEvent(this);
// no event parameter for e.consume();
}
To my questions:
Q1: When I use the action listener from the ToggleButton, both the "normal" click event as well as the long Press event are fired. I need to distinguish between the two though. The longPointerPress method does not have the event in the param list, hence I cannot consume the event after being done with my long Press Event activity. How can I prevent the normal action listener from firing?
Q2: For the "highlight effect", I would like the item to have a margin to all sides, overall shrinking the element by that amount. In other words, without increasing the previous total size. By just adding a margin though, the item gets bigger. How could a shrink a given element with a margin to all sides, but preserve the original size?
Q3: A FAB only has the option to "setIcon", not "setMaterialIcon". Hence, I am currently recreating the FAB every time it changes, as I dont want to hustle with the involved styles. Is there a better way than this?
//this is the unwanted function, as I dont want to set the style myself
fab.setIcon(FontImage.createMaterial(icon, s));
//Delete FAB pressed, change to Add FAB
fab.remove();
fab = FloatingActionButton.createFAB(FontImage.MATERIAL_ADD);
fab.bindFabToContainer(current.getContentPane(), Component.RIGHT, Component.BOTTOM);
Action event is always invoked on pointer release regardless of whether there was a long press event fired as we don't "know" you processed the longPress event. You would need to create a flag such as:
private boolean handledInLongPress;
public void longPress(int x, int y) {
// do your stuff
handledInLongPress = true;
}
private void handleActionEvent(ActionEvent ev) {
// I'm using this as a placeholder for your event code
// block the event from propagating and undo anything it might
// have triggered
ev.consume();
}
I would recommend using setUIID()
on the elements and define a "delete*" set of UIID's. You can define smaller padding and fonts to create the effect of shrinking but that might be tricky if you have icons here as well. You can scale down said icons and keep the original for restoring in a client property.
FAB makes a lot of assumptions so re-creating it (or using two instances) is probably a better approach than trying to set the icon. There is no way to change the icon of the FAB at runtime in the current implementation.