How do you enable a slider to scroll an entire panel?
30 views (last 30 days)
Show older comments
So far, I have only succeeded in scrolling the contents in the panel. However, I want the whole panel to scroll along with the content.
any form of help will be highly appreciated
Thanks in advance, Cordel Williams
0 Comments
Answers (3)
Walter Roberson
on 12 Jun 2013
If you have contents of an axes (lines, surfaces, drawn text) that you want to move while the axes itself stays in one location, then use panning to adjust what is visible in the axes (by either using the pan tool, or by adjusting the axes XLim and YLim)
When you have a collection of objects that are to be moved within a figure while the figure itself stays in one place, such as the position of axes, or position of controls relative to the figure edge, then put the collection inside a uipanel and have slides adjust the location of the uipanel relative to the figure.
The above is enough in most cases, but if you need a uipanel itself to move, then put the uipanel within a uipanel. And if you need that to move, put it within a uipanel. And so on. It's uipanels all the way down!
4 Comments
Walter Roberson
on 12 Jun 2013
p1 = uipanel(...)
p2 = uipanel(..., 'Parent', p1);
p2 is now contained within p1, and if you change the Position property of p2, you will be changing the position relative to p1
Selby
on 6 Dec 2016
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');
This uses Yair's findjobj
0 Comments
Jorrit M
on 8 Dec 2020
The above answers didn't work for me for a programmatically generated GUI in R2019b, and I struggled for an embarrasingly long time to get this stupid scrollbar to work, so I'll leave my solution here for future reference. Hopefully someone will find it useful.
My setup: 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. Anyway, godspeed and good luck:
%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
0 Comments
See Also
Categories
Find more on Migrate GUIDE Apps 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!