Java Callbacks with uitab call wrong callback

조회 수: 6 (최근 30일)
Marco H
Marco H 2015년 11월 2일
댓글: Marco H 2016년 3월 25일
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