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().getMenu().addMenuListener(new MenuListener() {
public void menuHidden(MenuEvent e) {

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

EBTCalc Users: A Desktop Version is Now Available!October 25, 2019
Python Script to Audit MediaMonkey TranscodingAugust 15, 2019
How to decompile Java code with JetBrains IntelliJ IDEA (2018.2.3, Windows 10)October 5, 2018
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