Java Callbacks with uitab call wrong callback
이전 댓글 표시
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개)
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:
카테고리
도움말 센터 및 File Exchange에서 App Building에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!