问题
I'm using uitab group in matlab in my GUI. However one limitation of the UItabgroup is absence of enable/disable feature.I tried to use other alternative by using a function from the matlab communnity findjObject
I use the following way to do that using the above function.
jtabgroup=findjobj(tabgroup);
jtabgroup.setEnableAt(false); % also I tried turning enable off for
% for individual tabs using setEnabledAt(1,0)
and I get the following error
Undefined function 'setEnabled' for input arguments of type 'handle.handle'.
Can someone help me with this issue or suggest me some alternative way of enable/disable tabs.
回答1:
You can, and should, use a wonderful GUI Layout Toolbox instead of uitab. It allows you to selectively disable tabs out of the box, not to mention the vast array of other useful features. There are two submissions on FEX, one for HG1 (uiextras package) and another for HG2 (uix package, with backward-compatible uiextras interface).
Here's HG2 example. You need to set TabEnables property to an array of 'on'/'off' values, one per tab (not the most user-friendly API, but hey, that's far better than anything else out there).
f = figure();
p = uix.TabPanel('Parent', f,'Padding', 5);
uicontrol('Style', 'frame', 'Parent', p, 'Background', 'r');
uicontrol('Style', 'frame', 'Parent', p, 'Background', [.8 .8 .8]);
uicontrol('Style', 'frame', 'Parent', p, 'Background', 'g');
p.TabTitles = {'Red', 'Gray', 'Green'};
p.Selection = 2;
p.TabEnables = {'on','on','off'};
Another suggestion would be to resort to pure java solutions. That obviously assumes you can only place java components inside your tabs, but pretty much any Matlab UI component, apart from axes, can be easily replaced with better-behaving and better-looking java counterpart.
回答2:
Unfortunately, as reported in the comments, the uitab does not have a enable property.
This is the list of the iutab object:
BackgroundColor
BeingDeleted
BusyAction
ButtonDownFcn
Children
CreateFcn
DeleteFcn
ForegroundColor
HandleVisibility
Interruptible
Parent
Position
SizeChangedFcn
Tag
Title
TooltipString
Type
UIContextMenu
Units
UserData
A possible workaround could be to enable / disable the uicontrols belonging to the uitab.
You can get the list of uicontrols of a uitab since they handles are stored in the children property.
Once you have the handles, you can set their enable property to either on or off.
In the following code:
- a
uitabgroupis created including twoiutab - in each
iutabsomeuicontrolsare added with the initialenablestatus set to 'off` - in the figure, two
checkboxare added to enable / disable theuicontrolsof the twouitab
The functin that get the hanles and enable / disable the uicontrols has been directly written as callbak of the checkbox
% Create a Figure
f = figure;
% Add a uitabgroup
tabgp = uitabgroup(f,'Position',[.05 .05 .3 .8]);
% Add two uitab
tab1 = uitab(tabgp,'Title','Tab #1');
tab2 = uitab(tabgp,'Title','Tab #2');
% Add some uicontrols to the first uitab (initial status = disabled)
% Add a Pushbutton
uicontrol('parent',tab1,'style','pushbutton','unit','normalized', ...
'position',[.1 .1 .3 .1],'string','OK','enable','off')
% Add a checkbox
uicontrol('parent',tab1,'style','checkbox','unit','normalized', ...
'position',[.1 .3 .6 .1],'string','Checkbox #1','enable','off')
% Add a radiobutton
uicontrol('parent',tab1,'style','radio','unit','normalized', ...
'position',[.1 .6 .5 .1],'string','Radio #1','enable','off')
% Add another radiobutton
uicontrol('parent',tab1,'style','radio','unit','normalized', ...
'position',[.1 .5 .5 .1],'string','Radio #2','enable','off')
% Add some uicontrols to the first uitab
% Add a Pushbutton
uicontrol('parent',tab2,'style','pushbutton','unit','normalized', ...
'position',[.1 .1 .3 .1],'string','OK','enable','off')
% Add a checkbox
uicontrol('parent',tab2,'style','checkbox','unit','normalized', ...
'position',[.1 .3 .6 .1],'string','Checkbox #1','enable','off')
% Add a radiobutton
uicontrol('parent',tab2,'style','radio','unit','normalized', ...
'position',[.1 .6 .5 .1],'string','Radio #1','enable','off')
% Add another radiobutton
uicontrol('parent',tab2,'style','radio','unit','normalized', ...
'position',[.1 .5 .5 .1],'string','Radio #2','enable','off')
% Add two checkbox to the Figure to enable / disable the uicontrols in the
% uitab
uicontrol('parent',f,'style','checkbox','unit','normalized', ...
'position',[.4 .3 .6 .1],'string','Enable Tab 1 uicontrols', ...
'callback','tab1_c=get(tab1,''children'');e_d=''off'';if(get(gcbo,''value'') == 1) e_d=''on''; end;set(tab1_c(:),''Enable'',e_d)')
uicontrol('parent',f,'style','checkbox','unit','normalized', ...
'position',[.4 .4 .6 .1],'string','Enable Tab 2 uicontrols', ...
'callback','tab2_c=get(tab2,''children'');e_d=''off'';if(get(gcbo,''value'') == 1) e_d=''on''; end;set(tab2_c(:),''Enable'',e_d)')
Hope this helps.
Qapla'
来源:https://stackoverflow.com/questions/37432008/enable-disable-uitabs-in-matlab