Assign callback to Java object prevents clearing of the MATLAB object
1 次查看(过去 30 天)
显示 更早的评论
Hi, I have my own CalibrateTable class used as a MATLAB interface for custom Java table (modified JTable). Purpose is to have everything in one place and prevent other developers to directly access Java stuff - they will use objects of this class when manipulating table data. Problem arises when I want to assign callbacks to the table - it gets executed but I'm then not able to clear CalibrateTable class by "clear classes" command ("Cannot clear this class or any of its superclasses..."). The problem persists even when I pass something completely different than the object itself.
I include small part of the code:
classdef CalibrateTable < handle
properties
jScrollPaneHandle
mTableHandle
jTableHandle
hTableHandle
%other properties..
end
methods
%some methods
%constructor
function obj = CalibrateTable(parent,position,tag)
javaaddpath('Table.jar','-v0');
rowtable = javaObjectEDT('Table');
parentScrollpane = javaObjectEDT('javax.swing.JScrollPane', rowtable);
[obj.jScrollPaneHandle, obj.mTableHandle] = javacomponent(parentScrollpane, position, parent);
obj.jTableHandle = javaObjectEDT(rowtable);
%get callback handle
obj.hTableHandle = handle(obj.jTableHandle,'CallbackProperties');
%add callback
obj.hTableHandle.MousePressedCallback = @(src,event)CalibrateTable.DummyCallback(obj,src,event);
end
end
methods(Static)
function DummyCallback(varargin)
disp('hello');
end
end
end
Any ideas? The class is cleared when I set the callback handle back to []/'' value so I've tried to explicitely define a constructor delete(obj) function but it is called only when the callback is set with no input arguments. onCleanup() does not seem to work for me as well.
Any help would be appreciated because I've already spent a ton of time on this issue...
edit> MATLAB 2010b/32bit/Win7
0 个评论
回答(1 个)
Lawrence
2024-3-15
It's hard to tell for sure without access to your java class, but I think the following will work:
I'm assuming your "parent" is a Matlab object, not a Java object? If so, you can avoid this by explicitly tying the lifetime of your object to the parent with addlistener(). Of course, you'll still need to make sure that your tables are all closed before clearing the path.
You may also have to still explicitly clear the callback on deletion. I haven't tested it enough to be sure.
classdef CalibrateTable < handle
properties
jScrollPaneHandle
mTableHandle
jTableHandle
hTableHandle
%other properties..
end
methods
%some methods
%constructor
function obj = CalibrateTable(parent,position,tag)
javaaddpath('Table.jar','-v0');
rowtable = javaObjectEDT('Table');
parentScrollpane = javaObjectEDT('javax.swing.JScrollPane', rowtable);
[obj.jScrollPaneHandle, obj.mTableHandle] = javacomponent(parentScrollpane, position, parent);
obj.jTableHandle = javaObjectEDT(rowtable);
%get callback handle
obj.hTableHandle = handle(obj.jTableHandle,'CallbackProperties');
%add callback
obj.hTableHandle.MousePressedCallback = @(src,event)CalibrateTable.DummyCallback(obj,src,event);
addlistener(parent,'ObjectBeingDestroyed',@(~,~)delete(obj));
end
function delete(obj)
obj.hTableHandle.MousePressedCallback = [];
end
end
methods(Static)
function DummyCallback(varargin)
disp('hello');
end
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Java from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!