Clear Filters
Clear Filters

Add CallbackFcn to checkbox jList

3 views (last 30 days)
Florian Bastiaens
Florian Bastiaens on 13 May 2015
Commented: Walter Roberson on 16 May 2015
Hi everyone,
I managed to create a checkboxlist using the following code.
% First create the data model
jList = java.util.ArrayList; % any java.util.List will be ok
for i = 1:Nrofmsrmnts
jList.add(java.lang.String(Measurement(i).name));
end
% Next prepare a CheckBoxList component within a scroll-pane
jCBList = com.mathworks.mwswing.checkboxlist.CheckBoxList(jList);
jScrollPane = com.mathworks.mwswing.MJScrollPane(jCBList);
% Now place this scroll-pane within a Matlab container (figure or panel)
%[jhScroll,hContainer] = javacomponent(jScrollPane,[10,50,200,650],gcf);
% Update some items' state programmatically
jCBModel = jCBList.getCheckModel;
jCBModel.uncheckAll;
% Respond to checkbox update events
jhCBModel = handle(jCBModel, 'CallbackProperties');
set(jhCBModel, 'ValueChangedCallback', @myMatlabCallbackFcn);
Now I can check what checkboxes are checked using:
checked = jCBList.getCheckedIndicies
This works, but when I check a box I get the error:
Undefined function 'myMatlabCallbackFcn' for input arguments of type 'javahandle_withcallbacks.com.mathworks.mwswing.checkboxlist.DefaultListCheckModel'.
I tried using
function myMatlabCallbackFcn
but then I get this error: Function definitions are not permitted in this context.
How do I define this callback function?

Answers (1)

Jan
Jan on 13 May 2015
Where did you try top create the function myMatlabCallbackFcn? You can define functions only inside function M-files, not inside scripts or inside the command window.
Teh callback gets an object of the type 'javahandle_withcallbacks.com.mathworks.mwswing.checkboxlist.DefaultListCheckModel'. Are you sure that your function can handle such an input?
  2 Comments
Florian Bastiaens
Florian Bastiaens on 16 May 2015
Thanks for your respons Jan.
You were right, I tried creating the function inside the script and after I fixed that I still couldn't figure it out. So I tried a different approach.
I created a button under the chechboxes and pressing this button runs a script. This script checks what boxes are checked and plots the data corresponding to the boxes. All parts work independent of eachother but not together. The problem is running the script when pressing the button. I get the error:
Attempt to add "plotting" to a static workspace.
How do I fix this? I can't make a script global.
Walter Roberson
Walter Roberson on 16 May 2015
Assign a value to "plotting" before you make whatever call results in that error message. It doesn't matter what you assign, as long as it exists.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!