PDA

View Full Version : Dynamic Context in Tree. How to Prevent show after Event ?



Mebel
21 Apr 2011, 12:21 AM
Hello,

i have a dynamic context menu on each node. Sometime its to possible to get a context menu without any items. Sadly the context menu show a clear rect on the screen.


How its possible to prevent this ?

Here my Code:


Menu contextMenu = new Menu();
tree.addListener(Events.ContextMenu, new Listener<ComponentEvent>() {
public void handleEvent(ComponentEvent be) {
Object selectedObject = tree.getSelectionModel().getSelectedItem();

Menu contextMenu = cfactory.createContextMenuselectedObject);
addNewEntriesMenu(contextMenu,selectedObject);

if (contextMenu.getItemCount()==0) {
//tree.setContextMenu(null); // cant set to null cause no more events fired.
// add usefull thing here to prevent show contextmenu
}
tree.setContextMenu(contextMenu);
}
});
tree.setContextMenu(contextMenu);

sven
21 Apr 2011, 12:54 AM
You can simple cancel the Events.ContenxtMenu event if you dont want to open the menu.

Mebel
21 Apr 2011, 1:14 AM
Hello Sven,

fast reply thx.
this code doesnt work. Any idea.



if (contextMenu.getItemCount()==0) {
be.stopEvent();
be.getEvent().stopPropagation();
return;
}



regards
me

sven
21 Apr 2011, 1:30 AM
because you have not cancelled it. None of the methods you called contain the word cancelled ;)

what about
be.setCancelled(true);

Mebel
21 Apr 2011, 1:34 AM
:D yes looks like a self created easter egg.

Work well now. thx.

Happy Easter
me