I have a Grid and need to enable some actions on individual values. For this I want to use an ActionMenu that shows on right-click of the grid cell.

This should behave just as the normal right-click menu. On a selection being made the action would evaluate and the ActionMenu would disappear. On clicking anywhere off of the ActionMenu it would disappear.

I am stuck on the first part of showing the ActionMenu. I know I need to use CurrentValue["MousePosition"] to position it but am not certain how to do this or how to get it to disappear.

A minimal Grid example is below.

    {{"MouseClicked", 2} :> 
      ActionMenu["Action List", {"Value" :> Echo@#1, "Index" :> Echo@#2}]}
    ] &,
  Partition[Range[4], 2],



1 Answer 1


Both solutions are undocumented but I don't think they will go away. Menu/MenuItem are used internally for a long time and AttachedCell is a relatively new concept but many of new features use it. E.g. DateSetter[], suggestion toolbar, image tools, message stack preview, etc.


Quite out of the box. One needs to remember to use KernelExecute for procedures which can not be performed by the FrontEnd. Also, MenuEvaluator must be set to Automatic or a specific name of the Kernel you want to use. Otherwise the FrontEnd will try to run the procedure and it will probably fail returning 'unknown packet dialog'. Why both are needed? I don't know.

Grid @ MapIndexed[
  Style[ #1, ContextMenu -> {
    MenuItem["Value", KernelExecute@Echo@#, MenuEvaluator -> Automatic], 
    MenuItem["Index", KernelExecute@Echo@#2, MenuEvaluator -> Automatic]
  }] &,
  Partition[Range[4], 2], {2}

enter image description here

Undocumented AttachedCell usage, more flexible

A small example:

menu = With[
     {ca := NotebookDelete@EvaluationCell[]}
  ,  MathLink`CallFrontEnd[ FrontEnd`AttachCell[
       , Cell[ BoxData @ ToBoxes @ ButtonBar[
              {"Value" :> (ca; Echo@#1), "Index" :> (ca; Echo@#2)}
            , Appearance -> "Vertical"
       , Center, Center, "ClosingActions" -> {"OutsideMouseClick"}
] &

Grid @ MapIndexed[
  EventHandler[#1, {{"MouseClicked", 2} :> (menu[#, #2])}] &, 
  Partition[Range[4], 2], {2}]

enter image description here

For more examples with an AttachedCell search around.

  • $\begingroup$ Let us continue this discussion in chat. $\endgroup$
    – Edmund
    Commented May 3, 2017 at 17:30
  • $\begingroup$ Not on this site often enough anymore. Only just read this. Style[...,ContextMenu...] is a good find. I've had to place a ContextMenu.tr file in the FrontEnd>TextResources path to customise the context menu but this is potentially a much better solution $\endgroup$ Commented Jan 6, 2019 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.