I need to create a popup Menu for a Tree in Java SWT. But the menu should only pop up when no items are selected (when I click on a blank space of the TreeViewer). If I now select an item of the tree, I can’t deselect it again. The TreeViewer is inside a Composite.
My first idea was to add a MouseListener to check if no of the Items are selected and call deselectAll(), but event.getSource() only returns the tree.
Any ideas of how to remove an item selection when a blank space is (right-) clicked?
I need to create a popup Menu for a Tree in Java SWT. But
Share
Often, you can deselect by Ctlr-clicking the item.
Another option is to register a listener for mouse clicks, and use the event location to locate the tree item. If this returns null, you can call
deselectAll().But how can you get the
TreeViewerfrom theTree? Simple: Store the reference in thedataproperty of the tree. Then you can use this code in your event handler: