以下测试代码尝试将 Matlab 上下文菜单链接到 javax.swing.JTable。JTable 显示正常,鼠标右键单击确实进入了适当的代码部分(在底部)。谁能告诉我我做错了什么?提前致谢!
function test
% Initialize
hMain = [];
% Set up figure window
initial_position = [100 300 925 475];
hMain.fMain = figure('NumberTitle','off',...
'name','Test Table',...
'Toolbar','no','menubar','no','DockControls','off',...
'Position',initial_position,'visible','on',...
'CloseRequestFcn',@CB_Project_Quit);
% Set up context menu
cmenu = uicontextmenu('Parent',hMain.fMain,...
'Interruptible','off','Visible','on','Tag','ContextMenu');
% Define the context menu items
item1 = uimenu(cmenu, 'Label', 'Test1');
item2 = uimenu(cmenu, 'Label', 'Test2');
item3 = uimenu(cmenu, 'Label', 'Test3');
% Create JTable
hMain.Tabs.hTable=javax.swing.JTable(8,8);
hMain.Tabs.hPanel=jcontrol(hMain.fMain, 'javax.swing.JScrollPane',...
'Units','pixels','Position', [12 12 900 450],...
'UIContextMenu',cmenu);
hMain.Tabs.hPanel.setViewportView(hMain.Tabs.hTable);
% Set up CallBack
tableHandle=handle(hMain.Tabs.hTable,'callbackproperties');
set(tableHandle,'MousePressedCallback',{@mousePressedCallback,cmenu});
% Close Window
function CB_Project_Quit(hObj,eventData)
delete(hObj);
% Turn on context menu at right mouse click (doesn't work!)
function mousePressedCallback(hObj, eventData, cmenu)
if eventData.isMetaDown % right-click is like a Meta-button
clickX = eventData.getX;
clickY = eventData.getY;
myPosition = [clickX clickY];
set(cmenu,'Position',myPosition,'Visible','on');
end