I want to add a scroll or slider to my menu in matlab

Viewed 26

I have a gui which list two types of things in the menubar. The solution that was found was to limit the number of elements in the context menu to 25 :

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% construction menu éléments
function base=CreeMenuElem(noms,base,pref)
global kg4_HndlFig
hm=findobj(kg4_HndlFig,'Tag','Elem0');
delete(get(hm,'children')) %reset du menu éléments
set(hm,'Enable','on') %activation du menu éléments
if pref.CacheElem
    noms=noms(~strncmp(noms,'@',1));    %suppression des éléments cachés
end
n=length(noms);
if pref.LimitMenu,MaxMenus=25;else MaxMenus=30;end
ChOld='';k=base;nm=0;
while (k<=n)
    Ch=noms{k};
    p=strfind(Ch,'::');
    if isempty(p)
        nm=nm+1; if nm>MaxMenus, break, end
        uimenu(hm,...
            'Label',Ch,...
            'Tag','Element',...
            'Callback','kgexec4');
        ChOld='';
    else
        ChNew=Ch(1:p-1);
        if ~strcmp(ChOld,ChNew)
            nm=nm+1; if nm>MaxMenus, break, end
            hb=uimenu(hm,'Label',ChNew);
            ChOld=ChNew;
        end
        uimenu(hb,...
            'Label',Ch,...
            'Tag','Element',...
            'Callback','kgexec4');
    end
    k=k+1;
end
if (base>1)|(k<n)
    uimenu(hm,...
        'Label',sprintf('(éléments %g à %g / %g) Suite...',base,k-1,n),...
        'Separator','on',...
        'Tag','SuiteElements',...
        'Callback','kgexec4');
end
if k>n
    base=1; %retour au début de la liste
else
    base=k;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% construction menu barres
function base=CreeMenuBarr(noms,base,pref)
global kg4_HndlFig
hm=findobj(kg4_HndlFig,'Tag','Barre0');
delete(get(hm,'children')) %reset du menu barres
set(hm,'Enable','on') %activation du menu barres
noms=noms(2:end);   %suppression de GND
if pref.CacheBarr
    noms=noms(~strncmp(noms,'@',1)); %suppression des noms auto
end
n=length(noms);
if pref.LimitMenu,MaxMenus=25;else,MaxMenus=30;end
bb=min(base+MaxMenus-1,n);  %dernier menu à afficher
for k=base:bb
    uimenu(hm,...
        'Label',noms{k},...
        'Tag','Barre',...
        'Callback','kgexec4' );
end
if (n>MaxMenus) && pref.LimitMenu
    uimenu(hm,'Label',sprintf('(barres %g à %g / %g) Suite...',base,bb,n),...
        'Tag','SuiteBarres', 'Separator','on', 'Callback','kgexec4' );
end
if bb<n, base=bb+1; else base=1; end %retour au début de la liste

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

I would like to be able to scroll in these menus instead of using this solution that can be impractical and slow but i don't understand how to add the scroll option. I tried with uicontrol but it never works.

1 Answers

Try

g=figure;
    u=uipanel(g,'backgroundcolor',[1 0 1],'units','pixels','position',[1000 1000 g.InnerPosition(3)-19 1000]);
    uu=findjobj(u);
    j=javax.swing.JScrollPane(uu);
    j.setHorizontalScrollBarPolicy(j.HORIZONTAL_SCROLLBAR_NEVER);
    [jj hh]=javacomponent(j,[10 10 g.InnerPosition(3)-20 g.InnerPosition(4)-20],g);
    %components to add to panel
    button=uicontrol(u,'style','pushbutton','units','pixels',...
        'position',[0.1 0.1 100 100],'string','blah');

or if that doesnt work this longer method from the MATLAB math works page

Example : I wish to create a panel with a variable number of lines and text edit boxes that the user can scroll through to edit. The main window handle is ptrMainGUI. I have two subpanels: one for the slider, and one for the content that it will scroll through: ptrPanelStimParamsParent and ptrPanelStimParams. intParams here is the number of lines I'm going to generate. As long as you create variables ptrMainGUI, as well as intParams, cellProps, cellVals, cellComments for your lines and values to generate in the panel I think it should work.

%get main GUI size and define subpanel size
ptrMainGUI.Units = 'pixels';
vecGuiSize = ptrMainGUI.Position;
ptrMainGUI.Units = 'normalized';
dblPanelHeight = 0.2;
dblPanelY = 0.1;
%calculate the total size of the subpanel content
dblTotSize = (intParams+1)*30;
dblRelSize = (dblTotSize/(vecGuiSize(end)*dblPanelHeight))+dblPanelHeight;
%create the panels
ptrPanelStimParamsParent = uipanel('Parent',sFigRE.ptrMainGUI);
set(ptrPanelStimParamsParent,'Position',[0.01 dblPanelY 0.94 dblPanelHeight]);
ptrPanelStimParams = uipanel('Parent',ptrPanelStimParamsParent);
set(ptrPanelStimParams,'Position',[0 0 1 dblRelSize]);
ptrSlider = uicontrol('Style','Slider','Parent',sFigRE.ptrMainGUI,...
    'Units','normalized','Position',[0.94 dblPanelY 0.05 dblPanelHeight],...
    'Value',1,'Callback',{@slider_callback1,ptrPanelStimParams});
%add all variables
vecParamTextPtrs = nan(1,intParams);
vecParamEditPtrs = nan(1,intParams);
for intParam=1:intParams
    vecParamTextPtrs(intParam) = uicontrol(ptrPanelStimParams,'style','text',...
        'Position',[1 (intParams*30)-((intParam-1)*30) 150 25],'String',cellProps{intParam},'FontSize',10);
    vecParamEditPtrs(intParam) = uicontrol(ptrPanelStimParams,'style','edit',...
        'Position',[150 (intParams*30)-((intParam-1)*30) 390 25],'String',cellVals{intParam},'Tooltip',cellComments{intParam},'FontSize',10);
end
%add callback
slider_callback1(ptrSlider,[],ptrPanelStimParams);

Now, to scroll properly we'll need to use the following function with some arcane transformations. The vecSize(end)-1 is to avoid showing an empty block the size of exactly one parent panel:

 function slider_callback1(hObject,eventdata,ptrSubPanel)
        vecSize = ptrSubPanel.Position;
        val = get(hObject,'Value');
        dblRealMax = vecSize(end) - 1;
        dblStartY = val*dblRealMax;
        vecSetPanelPos = [0 -dblStartY 1 vecSize(end)];
        set(ptrSubPanel,'Position',vecSetPanelPos);%[from-left from-bottom width height]
    end
Related