简体   繁体   中英

Can't get mouse callback to work with java swing

I would like to get eventData when the mouse is clicked or dragged along a slider. I have tried suggestions across blog posts ( http://undocumentedmatlab.com/blog/uicontrol-callbacks ) and the corresponding book. It seems to me that I am missing something. Here is the code:

function testMouse
hFig = figure('Position',[450 100 700 850]);

jSlider = javax.swing.JSlider;
[jSlider,~] = javacomponent(jSlider,[100,20,500,50]);
jbh = handle(jSlider,'CallbackProperties');

set (jbh, 'MouseDraggedCallbackData', @myCallbackFcn)

guidata(hFig, handles);

function myCallbackFcn
get (jbh, 'MouseDraggedCallbackData')
guidata(hFig, handles);

Here is the error I get when I run the code in MATLAB:

Error using javahandle_withcallbacks.javax.swing.JSlider/set Changing the 'MouseDraggedCallbackData' property of javahandle_withcallbacks.javax.swing.JSlider is not allowed.

Error in testMouse (line 8) set (jbh, 'MouseDraggedCallbackData', @myCallbackFcn)

I'd appreciate any suggestions.

Thanks to my colleague for sharing a simple way to get the final value from the slider. Here is the code:

function jSlider = testMouse2

hFig = figure('Position',[450 100 700 850]);

jSlider = javax.swing.JSlider;
jSlider.setMinimum(0);
jSlider.setMaximum(20);
jSlider.setMajorTickSpacing(5);
jSlider.setMinorTickSpacing(1);
jSlider.setPaintTicks(true);
jSlider.setPaintLabels(true);
jSlider.setSnapToTicks(true);
javacomponent(jSlider,[10,70,600,45]);

end

You can call the function, and then simply use a java call to get the final value of the slider this way:

slider = testMouse2;

slider.getValue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM