Java Callbacks with uitab call wrong callback
Show older comments
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
Answers (1)
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:
Categories
Find more on App Building 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!