Skip to content

Instantly share code, notes, and snippets.

@rcassani
Last active February 6, 2026 16:52
Show Gist options
  • Select an option

  • Save rcassani/4d4720aad61fcc704ab06e2b383bc1a7 to your computer and use it in GitHub Desktop.

Select an option

Save rcassani/4d4720aad61fcc704ab06e2b383bc1a7 to your computer and use it in GitHub Desktop.
Matlab Java JPopupMenu remains open in R2023b and newer when using New Desktop
% Create figure with mouse up callback
fig = figure('WindowButtonUpFcn', @FigureMouseUpCallback);
% Create popup menu on click place on right click
function FigureMouseUpCallback(hFig, ev)
import java.awt.MouseInfo.getPointerInfo
import javax.swing.JPopupMenu
import javax.swing.JMenuItem
if strcmpi(get(hFig, 'SelectionType'), 'alt')
% Get mouse XY
mouseInfo = java.awt.MouseInfo.getPointerInfo();
mouseLoc = mouseInfo.getLocation();
% Create JPopupMenu
jPopup = javaObjectEDT('javax.swing.JPopupMenu');
% Add items
itemText = {'A', 'B', 'C'};
for iItem = 1 : length(itemText)
jItems(iItem) = javaObjectEDT('javax.swing.JMenuItem');
jItems(iItem).setText(['Entry ' itemText{iItem}]);
hObj = handle(jItems(iItem), 'callbackProperties');
set(hObj, 'ActionPerformedCallback', @(h,ev)disp(['Selection: ' itemText{iItem}]));
jPopup.add(jItems(iItem));
end
% Show popup
jPopup.setLocation(mouseLoc);
jPopup.setInvoker(jPopup);
jPopup.setVisible(1);
drawnow;
end
end
@rcassani
Copy link
Author

rcassani commented Feb 6, 2026

R2023b_popupmenu_bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment