Class properties do not have values in uibutton callback

I have a uifigure and components in a class. I save 2 button handles as properties of the class: dockerButton and exitButton. When I click the dockerButton, the obj class I pass into its callback has an empty exitButton. When I click the exitButton, the obj class passed into its callback has both exitButton and dockerButton populated properly. I want this behavior for when I click the dockerButton; I want to be able to manipulate the exitButton that is a property of the obj instance passed in. What am I doing wrong?
When I click the dockerButton, it goes into dockView and my obj class is:
exitButton: [] <<-- I want this to have the handle to the exitButton!!
parent: [1×1 GridLayout]
borderPane: [1×1 Panel]
grid: []
title: 'GEO'
uuid: [1×1 java.lang.String]
cmapDir: [1×1 CmapDir]
dockerButton: [1×1 Button]
dockFile: 'arrow-collapse_16.png'
undockFile: 'arrow-expand_16.png'
When I click the exitButton, closeView gives me:
exitButton: [1×1 Button] <<-- I get both here!!
parent: [1×1 GridLayout]
borderPane: [1×1 Panel]
grid: []
title: 'GEO'
uuid: [1×1 java.lang.String]
cmapDir: [1×1 CmapDir]
dockerButton: [1×1 Button] <<-- so it's not a callback idiosyncracy
dockFile: 'arrow-collapse_16.png'
undockFile: 'arrow-expand_16.png'
classdef BaseView
properties
exitButton
parent
borderPane
grid
title
uuid
cmapDir
dockerButton
dockFile
undockFile
end % properties
methods
%% Constructor
function obj = BaseView(parent, title, cmapDir)
% parent is the grid from NextGenWorkspace
obj.parent = parent;
obj.title = title;
obj.cmapDir = cmapDir;
randomUUID = java.util.UUID.randomUUID;
obj.uuid = randomUUID.toString;
% start with a panel so I can get a nice outline. NOTE that
% even though HighlightColor is a property of uipanel, it only
% works when the parent is a figure (not a uifigure)
obj.borderPane = uipanel(obj.parent, ...
'BorderType', 'line', ...
'UserData', obj.uuid, ...
'Tag', title); %, ...
%'HighlightColor', [1 0 0]);
% 2 rows, 1 col
mainGrid = uigridlayout(obj.borderPane, ...
'tag', 'mainGrid');
mainGrid.RowHeight = {50,'1x'};
mainGrid.ColumnWidth= {'1x'};
% create header with title and close button
header = uipanel(mainGrid, ...
'BorderType', 'line', ...
'tag', 'header');
% 1 row, 2 col
hdrGrid = uigridlayout(header, ...
'tag', 'hdrGrid'); %, ...
% 'BackgroundColor',[0.7 0.7 0.7]);
hdrGrid.RowHeight = {33};
hdrGrid.ColumnWidth= {'1x',33,33};
uilabel(hdrGrid,'text',title);
% save icons that potenially can be used repeatedly
obj.dockFile = fullfile(pwd,'resources','arrow-collapse_16.png');
obj.undockFile = fullfile(pwd,'resources','arrow-expand_16.png');
windowCloseFile = fullfile(pwd,'resources','window-close_16.png');
% create dock button
if exist(obj.undockFile,'file') == 0
obj.dockerButton = uibutton(hdrGrid,'Text','>>');
else
obj.dockerButton = uibutton(hdrGrid, ...
'icon', obj.undockFile, ...
'text','');
end
set(obj.dockerButton, 'Tooltip', 'Undock View', ...
'userdata', 'docked=true', ...
'ButtonPushedFcn', @(src,event) dockView (obj,src,event));
% create close button
if exist(windowCloseFile,'file') == 0
obj.exitButton = uibutton(hdrGrid,'text','X');
else
obj.exitButton = uibutton(hdrGrid,'text','', ...
'icon', windowCloseFile);
end
set(obj.exitButton, 'Tooltip', 'Remove View', ...
'ButtonPushedFcn', @(src,event) closeView (obj,src,event));
% now the main workspace area where sub classes will put
% widget
obj.grid = uigridlayout(mainGrid, ...
'tag', 'grid');
obj.grid.RowHeight = {'1x'};
obj.grid.ColumnWidth= {'1x'};
end % function
end % methods
methods (Access = protected)
% callback for the close button
function closeView(obj, ~, ~)
disp ('BaseView::closeView');
end
% pop out the view into its own dialog or return it to its tab
function dockView(obj, ~, ~)
disp ('BaseView::dockView');
end
end % dockView
end % methods
end % classdef

댓글 수: 1

I figured it out. It's an ordering thing. Setting the ButtonPushedFcn callback BEFORE the exitButton was created resulted in me not having access to that button handle in the callback. Once I moved the line where I set my dockerButton callback to after the exitButton has been defined, everything worked as expected.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Desktop에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2022년 9월 7일

댓글:

2022년 9월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by