Eric Bergman-Terrell's Blog

Java Programming Tip: Avoid Automatically Selecting the First Item in a Context Menu on Ubuntu
January 16, 2009

On Ubuntu, when I right-click a TreeViewer or TextViewer to display the context menu, the first menu item is automatically clicked. The mouse click that brings up the context menu must be passed on to the menu itself. At any rate, if the first item in a context menu is enabled, it is implicitly clicked.

Here's the workaround, my application moves the cursor one pixel up, and one pixel to the left, to avoid clicking on the first menu item:

...
getControl().setMenu(menuManager.createContextMenu(parent));

getControl().getMenu().addMenuListener(new MenuListener() {
@Override
public void menuHidden(MenuEvent e) {
}

@Override
public void menuShown(MenuEvent e) {
// Move the cursor over a bit to avoid selecting the first menu item on Ubuntu.
Point cursorLocation = e.display.getCursorLocation();

Display.getCurrent().setCursorLocation(cursorLocation.x + 1, cursorLocation.y + 1);
}
});
...

This is a bit of a kludge, but it seems to look and work well on Ubuntu and Windows too.

Keywords: SWT, JFace, Java, Eclipse, TreeViewer, TextViewer, Context Menu

Reader Comments

Comment on this Blog Post

Recent Posts

TitleDate
Vault 3 Security EnhancementsOctober 24, 2023
Vault 3 is now available for Apple OSX M2 Mac Computers!September 18, 2023
Vault (for Desktop) Version 0.77 ReleasedMarch 26, 2023
EBTCalc (Android) Version 1.44 is now availableOctober 12, 2021
Vault (Desktop) Version 0.72 ReleasedOctober 6, 2021
EBT Compass is Now Available for Android DevicesJune 2, 2021
Convert a Windows 10 Notebook into a High-Capacity Photo FrameApril 3, 2021