Effacer les filtres
Effacer les filtres

Add CallbackFcn to checkbox jList

3 vues (au cours des 30 derniers jours)
Florian Bastiaens
Florian Bastiaens le 13 Mai 2015
Commenté : Walter Roberson le 16 Mai 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?

Réponses (1)

Jan
Jan le 13 Mai 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 commentaires
Florian Bastiaens
Florian Bastiaens le 16 Mai 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 le 16 Mai 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by