Introduction

There are two ways to register a context menu with the graphics view. The standard way by calling GraphicsBase.setContextMenu(ContextMenu) or by registering a context menu callback by calling GraphicsBase.setContextMenuCallback(). The advantage of the second option is that a parameter object of type ContextMenuParameter will be passed to the callback method. This paramter object contains the most relevant parameters that most context menus will require in order to let the user perform some kind of action on the graphics view.

Please note that a context menu callback will have precedence over a standard context menu.

Code Example

The following snippet shows an example of a context menu callback implementation. Here we simply add a menu item for each activity that was found at the mouse location where the context menu was requested by the user.

Context Menu Callback
GraphicsBase<?> graphics = ganttChart.getGraphics();
graphics.setContextMenuCallback(param -> {
	ContextMenu menu = new ContextMenu();
	for (ActivityRef<?> ref : param.getActivities()) {
		Activity activiy = ref.getActivity();
		MenuItem item = new MenuItem("Move " + activity.getName());
		item.setOnAction(evt -> moveActivity(activity);
		menu.getItems().add(item);
	}
	return menu;
});