Java Callbacks with uitab call wrong callback

1 view (last 30 days)
Marco H
Marco H on 2 Nov 2015
Commented: Marco H on 25 Mar 2016
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 Comment
Marco H
Marco H on 25 Mar 2016
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

Sign in to comment.

Answers (1)

Yair Altman
Yair Altman on 3 Nov 2015
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 Comment
Marco H
Marco H on 3 Nov 2015
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.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks 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!