Java Callbacks with uitab call wrong callback

6 次查看(过去 30 天)
Hi, I am writing a GUI application where I want to use java callbacks for some elements, especially for Mouse Interaction in plots. When I combine java callbacks with Matlab's uitabs, the callback only executes for the last tab in the uitabgroup. No matter which tab is active.
Here is a small example to demonstrate the problem. It requires findjobj from the FileExchange. If one clicks into the window, the output shows "Tab 2", even in Tab 1.
fig = figure;
tabbar = uitabgroup(fig);
tab1 = uitab(tabbar, 'Title', 'Tab 1');
tab2 = uitab(tabbar, 'Title', 'Tab 2');
panel1 = uipanel('Parent', tab1, 'Position', [0.25 0.25 0.5 0.5]);
axes('Parent', panel1);
panel2 = uipanel('Parent', tab2, 'Position', [0.25 0.25 0.5 0.5]);
axes('Parent', panel2);
jhpanel1 = handle(findjobj(panel1), 'CallbackProperties');
set(jhpanel1, 'MouseClickedCallback', @(h,e)disp('TAB 1'));
jhpanel2 = handle(findjobj(panel2), 'CallbackProperties');
set(jhpanel2, 'MouseClickedCallback', @(h,e)disp('TAB 2'));
Does anyone know the reason for this, or even has a workaround? I am using Matlab 2015a
  1 个评论
Marco H
Marco H 2016-3-25
For all others with a similar problem. the function findjobj finds the related java object by its position and size, as described in fileExchange. For that reason the second call of findjobj will return both handles, as they have the same position and the same size. The second call to "set" then overwrites the previous set of Callback with "TAB 1" to the one with "TAB 2".
Giving the object different positions or give findjobj more search parameters solves the problem

请先登录,再进行评论。

回答(1 个)

Yair Altman
Yair Altman 2015-11-3
Tab buttons are not independent ui controls, they are sub-components of the tab-group control. Therefore, findjobj(tab1) basically returns the content panel that is correlated to tab1, not the tab button itself (I explain this optical illusion in my Matlab-Java book). Anyway, if you use findjobj(tabbar) and select the last returned element, it should get you what you wanted. However, there is an even faster/better way, and that is to use the SelectionChangedFcn callback of the tab group handle itself:
set(tabbar, 'SelectionChangedFcn', @(h,e)disp(h.SelectedTab));
Additional details:
  1 个评论
Marco H
Marco H 2015-11-3
I am not sure if I understand your answer. I am not intrested in the Buttons of the tabgroup.
>> findjobj(tab1) basically returns the content panel that is correlated to tab1
This is exactly what I want, a Mouse Event on the content of a tab. I have clarified the example in my question and added a figure in a panel. A click on the figure displays allways Tab 2. No matter which tab is active. This is also true for MouseDraggedCallback or MouseMovedCallback
So my assumption is that, for some reason, the mouse events are passed through to the last tab with the corresponding callback defined.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 App Building 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by