jeudi 24 septembre 2015

How to programmacally implement a dynamic GUI with checkboxes and scrollbars?

i desperately try to create a GUI programmatically. It shall show the following things:

enter image description here

where the checkboxes at the right side are static!! the number of checkboxes on the left depends on the userinput!

What i try to do is to create is a panel for the dynamic checkboxes on the right hand side and a scrollbar for those panels.

Unfortunately i am completely new to programmatic GUI creation. So far everything worked fine with GUIDE.

So i woulde be glad for your help.

I found a good example

Adding scroll bar in subplots within GUI

for getting into it with axes but i dont get it adapted to checkboxes:( This is my Try so far.

%# create figure, panel, and slider
w = 600; h = 500;           %# width/height of figure
handles.hFig = figure('Menubar','figure', 'Resize','off', ...
    'Units','pixels', 'Position',[200 200 w h]);
handles.hPan = uipanel('Parent',handles.hFig, ...
    'Units','pixels', 'Position',[0 0 w-20 h]);
handles.hSld = uicontrol('Parent',handles.hFig, ...
    'Style','slider', 'Enable','off', ...
    'Units','pixels', 'Position',[w-20 0 20 h], ...
    'Min',0-eps, 'Max',0, 'Value',0, ...
    'Callback',{@onSlide,handles.hPan});

%# add checkbox

hcb = zeros(7,1);
clr = lines(7);

for i=1:7

    hcb(i) = addcheckbox(handles);

    pause(1)   %# slow down so that we can see the updates

end 

The slider function is not changed. And here is the most problematic function for me:

function hcb = addcheckbox(handles)
    %# look for checkboxes
    cb = findobj(handles.hPan, 'type','checkbox');

    if isempty(cb)
        %# create first checkbox

        hcb = uicontrol(handles.hFig,'Style','checkbox',...
                'String','Display file extension',...
                'Value',1,'Position',[30 20 130 20]);

    else
        %# get height of figure
        p = get(handles.hFig, 'Position');
        h = p(4);

        %# increase panel height, and shift it to show new space
        p = get(handles.hPan, 'Position');
        set(handles.hPan, 'Position',[p(1) p(2)-h p(3) p(4)+h])

%         %# compute position of new axis: append on top (y-shifted)
%         p = get(ax, 'Position');
%         if iscell(p), p = cell2mat(p); end
%         p = [p(1,1) max(p(:,2))+h p(1,3) p(1,4)];
% 
%         %# create the new axis
%         hAx = axes('Parent',handles.hPan, ...
%             'Units','pixels', 'Position',p);
% 
%         %# adjust slider, and call its callback function
%         mx = get(handles.hSld, 'Max');
%         set(handles.hSld, 'Max',mx+h, 'Min',0, 'Enable','on')
%         %#set(handles.hSld, 'Value',mx+h)       %# scroll to new space
%         hgfeval(get(handles.hSld,'Callback'), handles.hSld, []);
    end

end

When i execute it i get the error:

Error using findobj
Invalid handle

Error in addcheckbox (line 3)
    cb = findobj(handles.hPan, 'type','checkbox');

This error makes sense becaue i dont have a handle named checkboxes...in the example 'axes' is a predefined handle contained in the figure...how can i integrate checkboxes in this context?

I would be soo glad for your help!! Best regards, John




Aucun commentaire:

Enregistrer un commentaire