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
Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016
IPv6 HumorDecember 10, 2015
Java Programming Tip: Specify the JVM time zoneDecember 7, 2015
Node.js / Express Programming Tip: Detect and Fix Memory LeaksOctober 27, 2015