将Matlab中的uicontextmenu与Java Swing JTable相关联

时间:2013-07-01 20:28:35

标签: java matlab jtable contextmenu jscrollpane

以下测试代码是尝试将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

0 个答案:

没有答案