Azzera filtri
Azzera filtri

Assign callback to Java object prevents clearing of the MATLAB object

4 visualizzazioni (ultimi 30 giorni)
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

Risposte (1)

Lawrence
Lawrence il 15 Mar 2024
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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by