Last active
February 6, 2026 16:52
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| % 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 |
Author
rcassani
commented
Feb 6, 2026

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