How to remove callback from a java component?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Tong Wang
am 14 Jun. 2018
Kommentiert: Tong Wang
am 15 Jun. 2018
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) .
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/191256/image.png)
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 Kommentare
Adam
am 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.
Akzeptierte Antwort
Adam
am 15 Jun. 2018
Bearbeitet: Adam
am 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.
Weitere Antworten (1)
Adam
am 14 Jun. 2018
Bearbeitet: Adam
am 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 Kommentare
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!