Clear Filters
Clear Filters

How to remove callback from a java component?

3 views (last 30 days)
As you can see, i have here a java slider in matlab gui, i need to set a 'MouseReleasedCallback' when i use mouse to control the slider, then i also need a 'StateChangedCallback' for edit box on the right side (Number changes in edit box won't lead to 'MouseReleasedCallback', because there is no mouse action) .
The mindset is that, in initialization of this jslider a 'MouseReleasedCallback' will be set, then when edit box is called, a 'StateChangedCallback' for jslider will be created, then it leads to that callbackfunc, use guidata to save result, after that the 'StateChangedCallback' will be removed.(Or there will be two callback properties for one slider) i have code like this, the" set(handles.jsh,'StateChangedCallback',{}) " seems doesn't work. I suppose it should be a combination of java statement with matlab code , i have searched it in web for a whole day, but just no results, could anyone please give me a tip to completely remove this callback?
function Slider1_Edit_Callback(hObject, eventdata, handles)
set(handles.jSlider1,'StateChangedCallback',{@mycallback_jSlider1,handles})
TextValue = str2double(get(handles.Slider1_Edit,'String'));
set(handles.jSlider1,'Value',TextValue);
guidata(hObject,handles);
set(handles.jSlider1,'StateChangedCallback',{})
guidata(hObject,handles);
end
EDIT: Sorry for unclear description.
With the slider value changing in the edit callback function above should lead to the slider callback @mycallback_jSlider1, which is
function mycallback_jSlider1(hObject, eventdata, handles)
SliderValue = get(handles.jSlider1,'Value');
Images = evalin('base','Images');
imshow(Images{SliderValue},'Parent',handles.axes1);
end
By doesn't work i mean: I set breakpoints to @mycallback_jSlider1, when i change number in edit box, it shows that the @mycallback_jSlider1 was never be called...More stranger is that if i set breakpoint in @Slider1_Edit_Callback in lines before "set(handles.jSlider1,'StateChangedCallback',{})", it will call @mycallback_jSlider1, but if i set breakpoint after "set(handles.jSlider1,'StateChangedCallback',{})", it shows @mycallback_jSlider1 wasn't be called.....
  2 Comments
Adam
Adam on 14 Jun 2018
Ok, I'm confused what you are trying to do now. Shouldn't the link between the edit box and the slider be permanent? I don't understand why you are trying to set a callback for the slider inside the callback of what I assume is the editbox.
Surely you should just be setting the edit box callback to update the slider, not to set its callback and then clear it again?
If you want to call a function in response to both the edit box changing and the slider changing then you should simply call that function in permanent callbacks rather than setting up a temporary callback that you hope to trigger.
I realise this does cause some issues with java sliders though as you can get a double callback, once when the edit box changes and then once when the slider changes as a result of the edit box.
But in this case you shouldn't need to actually call your image update function in the edit box callback. Java sliders trigger their callback when they are programmatically updated so your edit box callback should just update the slider value. Your slider callback should be set once and then remain and should call your image update function as well as updating the edit box, ensuring you avoid the infinite loop of edit box and slider constantly updating each other.
Tong Wang
Tong Wang on 14 Jun 2018
Edited: Tong Wang on 14 Jun 2018
Thanks for your comment.
Ideally, the link between the edit box and the slider should be permanent, the task of edit box should just be upgrading the slider value. But as i wrote, in the initialization of this slider i have set a 'MouseReleasedCallback', then changing the edit box won't lead to slider callback, because i just type the number in keyboard without mouse action, i could change the slider value, but the callback of slider won't be done.
I can link to slider callback from edit box if i add a 'StateChangedCallback', but then i will have two callbacks simultaneously, if i use mouse to control slider now, the callback will immediately be called if there is a 'StateChanged',but this is in contradiction with my design, i just want to link to the slider callback only if i release the mouse. So in a word, in the end the 'StateChangedCallback' need to be deleted
Due to this reason i would like to set a temporary callback in edit box, before setting slider value , first create 'StateChangedCallback', then setting slider value, go to slider callback, show image, finally delete 'StateChangedCallback', make sure 'StateChangedCallback' won't appear with 'MouseReleasedCallback' together.
Thanks for your help

Sign in to comment.

Accepted Answer

Adam
Adam on 15 Jun 2018
Edited: Adam on 15 Jun 2018
Ok, I understand the problem now. I'll leave my previous answer in since it was related to the question in the title, even though it doesn't really add much, but to answer the new question:
Add a 'drawnow' instruction after setting the slider value before you remove its callback:
function Slider1_Edit_Callback(hObject, eventdata, handles)
set(handles.jSlider1,'StateChangedCallback',{@mycallback_jSlider1,handles})
TextValue = str2double(get(handles.Slider1_Edit,'String'));
set(handles.jSlider1,'Value',TextValue);
drawnow
set(handles.jSlider1,'StateChangedCallback',{})
end
I removed the guidata calls as they don't seem to be necessary there.

More Answers (1)

Adam
Adam on 14 Jun 2018
Edited: Adam on 14 Jun 2018
set(handles.jsh,'StateChangedCallback',[])
seems to work fine for me when I tested it out on a java slider, using square brackets to denote empty rather than using {} for a cell argument.
(That said, using {} works for me too to remove a callback)
What exactly is handles.jsh? For my test I just did the following:
figure;
jSlider = uicomponent( 'Style', 'JSlider' );
using the FileExchange uicomponent wrapper to create a JSlider. The first argument returned by that is the Matlab wrapper for the slider and setting the callback to empty on this works fine.
I thought I had some examples in my code of creating a raw java slider too, but it seems even in my early test code for java components I was using uicomponent to create it and return me the Matlab wrapper object.
Edit: About my 4th edit of this post - I have now also tested using the JSlider object itself and again both this syntax and yours, using {}, both work for me to remove a callback. What do you mean by 'doesn't work'? Does it crash or simply not remove the previous callback?
  2 Comments
Tong Wang
Tong Wang on 14 Jun 2018
hi, thanks for your answer, i have reedited my description, please take a look
Tong Wang
Tong Wang on 14 Jun 2018
the 'StateChangedCallback' is indeed removed, but my @mycallback_jSlider1 was never be called...

Sign in to comment.

Categories

Find more on Startup and Shutdown 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!