File Exchange

image thumbnail

attachScrollPanelTo - add scroll-panel to a uipanel or axes

version 1.0.0 (80.3 KB) by Yair Altman
Places the specified control/uipanel inside a scroll-panel that automatically appears when the container shrinks (resizes)


Updated 25 Jul 2018

View License

[hScrollPanel, hPanel] = attachScrollPanelTo(hObject);

attachScrollPanelTo places the specified control/uipanel handle in a scroll-panel (a Java JScrollPanel object). If the specified handle is not a uipanel, then it is placed inside a tightly-fitting borderless uipanel, which is then placed within the new scroll-panel.

The new scroll-panel automatically resizes with its containing figure/uipanel or other container (the specified handle's original parent). Scrollbars automatically appear as needed, when the container shrinks or expands.

The returned hScrollPanel can be seperately customized (for example, programmatically setting the viewport's ViewPosition): see usage examples below

The returned hPanel is the Matlab panel containing the input hObject. When hObject is a uipanel, hPanel==hObject; otherwise, hPanel is the tightly-fitting borderless panel that is created for the scroll-panel.

Calling attachScrollPanelTo with no input handle displays a demo.

attachScrollPanelTo() %display the demo
attachScrollPanelTo(hPanel) %place the specified panel in a scroll-panel

hScroll = attachScrollPanelTo(hPanel);
hScroll.ViewOffset = [30,50]; %set viewport offset (30px right, 50px down)
set(hScroll, 'ViewOffset',[30,50]); %equivalent alternative

- HG2 figures created with GUIDE or the figure command - works ok
- HG2 figures created with AppDesigner or uifigure command - does NOT work
- HG1 figures created on R2014a or older - does NOT work

Technical details:

This utility relies on undocumented functionality. It works on Matlab R2014 onward, but may fail to work at some future release. Use at your own risk!

Cite As

Yair Altman (2021). attachScrollPanelTo - add scroll-panel to a uipanel or axes (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (3)

Will Feavyour


Dear Yair

Many thanks for the useful extension.

I have a question about how it can be used in conjunction with a JTextArea (created using uicomponent)? Using the code below, I was only able to scroll through a limited range of the text. Is it possible to adjust the scroll limits so that the entire text can be seen? Any advice would be greatly appreciated!



%create a random string
s = 'a':'z';
textString = ['start ' s(randi(26,1,10000)) ' end'];

%create figure
hFig = figure(1);
hFig.OuterPosition=[0 0 1 1];

%create panel
hPan = uipanel('FontSize',12,...
'Position',[0 0 1 1]);
pos = getpixelposition(hPan);

%create textarea
jTA = uicomponent('Parent',hPan,'style','javax.swing.jtextarea','tag','myObj',...
'BackgroundColor',[0.6 0.6 0.6],'Opaque',0,'LineWrap',1);
jTA.Font = java.awt.Font('Helvetica', java.awt.Font.PLAIN, 22); % font name, style, size
jTA.Foreground = java.awt.Color(1,1,1);

%attach scroll panel
hSP = attachScrollPanelTo(jTA);

Markus Klingenberger

Thanks for this extension - it works almost perfect.

The only issue I have is that putting a panel in a tab and then attaching the scroll bar does not work well. Here is a minimal example:

app.fig = figure();
app.tabgroup = uitabgroup(app.fig); = uitab(app.tabgroup);
app.panel = uipanel(, 'Unit', 'pixels');
app.scroll = attachScrollPanelTo(app.panel);
app.panel.Position = [0 0 640 3000];
uicontrol(app.panel, 'style', 'pushbutton', 'position', [0 0 66 66]);
uicontrol(app.panel, 'style', 'pushbutton', 'position', [0 2944 66 66]);

The up button of the scrollbar is hidden behind the tab headers and when increasing the width of the figure (and thus the tab), the scroll bar moves more and more towards the outside of the figure and finally disappears.

Is there any way to make it work with uitabgroup/uitab? If not I will just drop the official uitabgroup/uitab and use some workaround to achieve what tabs do.

MATLAB Release Compatibility
Created with R2017b
Compatible with R2014b and later releases
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!