Changing the cursor shape in a popup menu?

In a project, we change the cursor from an arrow to a hand for all components that are clickable.
In this same project, we also use the function 'system.gui.createPopupMenu'.
Is it possible to change the cursor shape of the items in this popup menu to a hand when the item is enabled?

The menu object you create with system.gui.createPopupMenu will be a JPopupMenu. As a MenuElement it will have a getSubElements() method that returns the list of defined sub elements. Each of those, as a JComponent, has a setCursor method. To obtain a cursor, use the static method getPredefinedCursor on the Cursor class, using one of the defined constants.

3 Likes

Could you please create a working example script? It’s all still very new to me.

I can do that:

Example:

# Recursively sets the cursor on all the sub components of a popup menu
def setMenuCursor(menu, cursor):
	
	# Set the current menu item's cursor property to the given cursor
	menu.cursor = cursor	
	
	# Iterate through all the menu item's sub componentns, and apply the cursor
	for component in menu.components:
		setMenuCursor(component, cursor)

		# If a submenu is found, process the submenu items as well
		if 'JySubMenu' in component.__class__.__name__:
			setMenuCursor(component.popupMenu, cursor)


# Simple nested popup example based on documented example # 4:
def sayHello(event):
	print 'Hello'
subMenu01 = [["Click Me 1", "Click Me 2"], [sayHello, sayHello]]
subMenu02 = [["Click Me 3", "Click Me 4"], [sayHello, sayHello]]
subMenu03 = [["Click Me 5", "Click Me 6"], [sayHello, sayHello]]
subMenu04 = [["Click Me 7", "Click Me 8"], [sayHello, sayHello]]
subMenu05 = [["Click Me 9", "Click Me 10"], [sayHello, sayHello]]

# Create a popup menu and change its subcomponents' cursors prior to displaying it
menu = system.gui.createPopupMenu(['Click Me', 'subMenu01', 'subMenu02', 'subMenu03', 'subMenu04', 'subMenu05'], [sayHello, subMenu01, subMenu02, subMenu03, subMenu04, subMenu05])

# get the original cursor, and derive a hand cursor using the original cursor's static HAND_CURSOR field
originalCursor = menu.cursor
newCursor = originalCursor.getPredefinedCursor(originalCursor.HAND_CURSOR)

setMenuCursor(menu, newCursor)
menu.show(event, event.x, event.y)

Result:

Edit: Moved the derived cursor outside of the function, and added it as a variable, to make the function more generic

2 Likes

Thank you very much for this example.

1 Like