<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>uipanel &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/uipanel/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Wed, 01 Aug 2018 18:00:13 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.3</generator>
	<item>
		<title>Customizing web-GUI uipanel</title>
		<link>https://undocumentedmatlab.com/articles/customizing-web-gui-uipanel?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=customizing-web-gui-uipanel</link>
					<comments>https://undocumentedmatlab.com/articles/customizing-web-gui-uipanel#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 01 Aug 2018 18:00:13 +0000</pubDate>
				<category><![CDATA[Guest bloggers]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[AppDesigner]]></category>
		<category><![CDATA[Iliya Romm]]></category>
		<category><![CDATA[Khris Griffis]]></category>
		<category><![CDATA[uifigure]]></category>
		<category><![CDATA[uipanel]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7804</guid>

					<description><![CDATA[<p>We can customize Matlab's new web-based GUI  panels in many interesting ways. Here's how... </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-web-gui-uipanel">Customizing web-GUI uipanel</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/customizing-uifigures-part-3" rel="bookmark" title="Customizing uifigures part 3">Customizing uifigures part 3 </a> <small>As I have repeatedly posted in recent years, Matlab is advancing towards web-based GUI. The basic underlying technology is more-or-less stable: an HTML/Javascript webpage that is created-on-the-fly and rendered in a stripped-down browser window (based on Chromium-based jxBrowser in recent...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-uifigures-part-2" rel="bookmark" title="Customizing uifigures part 2">Customizing uifigures part 2 </a> <small>Matlab's new web-based uifigures can be customized using custom CSS and Javascript code. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-matlab-uipanels" rel="bookmark" title="Customizing Matlab uipanels">Customizing Matlab uipanels </a> <small>Matlab uipanel controls can be customized using Java in ways that are impossible with plain Matlab. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-axes-part-3-backdrop" rel="bookmark" title="Customizing axes part 3 &#8211; Backdrop">Customizing axes part 3 &#8211; Backdrop </a> <small>Matlab HG2 axes can be customized in many different ways. This article explains some of the undocumented aspects. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p><i>I would like to introduce guest blogger <a href="https://github.com/Khlick" rel="nofollow" target="_blank">Khris Griffis</a>. Today, Khris will continue the series of posts on <a href="https://undocumentedmatlab.com/articles/tag/uifigure" target="_blank">web-based uifigure customization</a> with an article showing how to create scrollable/customizable panels in web-based uifigures. This post follows <a href="https://undocumentedmatlab.com/articles/scrollable-gui-panels" target="_blank">last-week&#8217;s article</a>, about placing controls/axes within a scroll-panel in non-web (Java-based) figures. Users interested in advanced aspects and insights on the development roadmap of web-based Matlab GUI should also read <a href="https://blogs.mathworks.com/loren/2018/07/25/the-state-of-app-building-in-matlab" rel="nofollow" target="_blank">Loren Shure&#8217;s blog post from last week</a>.</i></p>
<h3 id="Motivation">Motivation</h3>
<p>As a retinal physiologist, I spend a lot of time in Matlab creating GUIs to visualize and analyze electrophysiological data. The data often requires a lot of processing and quality control checks before it can be used for interpretation and publication. Consequently, I end up with many control elements taking up precious space on my GUI.<br />
In Java-based (legacy/GUIDE) figures, this wasn&#8217;t a huge problem because, depending on what GUI components I needed, I could use a pure Matlab approach (a child panel within a parent panel, with a couple of control sliders moving the child panel around), or a number of Java approaches (which are always more fun; Yair <a href="https://undocumentedmatlab.com/articles/scrollable-gui-panels" target="_blank">described such an approach</a> last week).<br />
Unfortunately, the web-based (App-Designer) figure framework doesn&#8217;t support Java, and the pure/documented Matlab approach just doesn&#8217;t look good or function very well:<br />
<!--
If you're like me, you've once (or twice) queried your favorite search engine with "<i>Matlab scrolling uipanel</i>" and found 2 useful methods: a pure Matlab approach and <a href="https://undocumentedmatlab.com/articles/scrollable-gui-panels" target="_blank">a fun Java approach</a> (sticking objects in a <code>JScrollPane</code>, as explained in last week's post). Now, we read <i>Undocumented Matlab</i>, so we typically opt for the Java approach and our app is beautiful and the world is right... and countless hours of my life will go completely unnoticed by everyone using my app. But then the internet gets fast enough, servers get big enough, or for whatever reason, browser-/web-based apps are now <i>the</i> thing, so The Mathworks Inc. went and developed the <i><b>uifigure</b></i> to, you know, catch up.
The problem is, the new web framework eliminates our ability to use the Java approach and severely dampens the aesthetic of a pure Matlab approach (a child panel within a parent panel, with a couple of control sliders moving the child panel around). And that approach was already pretty ugly, if you care about that kind of stuff. So how can we make a scrolling <i><b>uipanel</b></i> to hold all these fancy, new <i><b><a href="https://www.mathworks.com/help/matlab/ref/uiknob.html" rel="nofollow" target="_blank">uiknob</a></b></i> controls?
--><br />
<center style="font-size:12px;"><img fetchpriority="high" decoding="async" src="https://undocumentedmatlab.com/images/uipanel_badscrolls.png" alt="AppDesigner uislider is not a good scrollbar, no matter what we do to it!" title="AppDesigner uislider is not a good scrollbar, no matter what we do to it!" width="473" height="187" /><br />
AppDesigner <i><b>uislider</b></i> is not a good scrollbar, no matter what we do to it!</center><br />
Fortunately, the new web framework allows us to use HTML, CSS and JavaScript (JS) to modify elements in the <i><b>uifigure</b></i>, i.e. its web-page <a href="https://en.wikipedia.org/wiki/Document_Object_Model" rel="nofollow" target="_blank">DOM</a>. It turns out that the <i><b>uipanel</b></i> object is essentially just a <code>&lt;div&gt;</code> with a Matlab-designed CSS style. We can customize it with little effort.<br />
The main goal here is to create a scrollable customizable <i><b>uipanel</b></i> containing many uicontrol elements, which could look something like this:<br />
<center style="font-size:12px;"><img decoding="async" src="https://undocumentedmatlab.com/images/uipanel_ADfig_1.png" alt="Running app demo" title="Running app demo" width="450" height="355" style="max-width:804px;"/></center><br />
<span id="more-7804"></span></p>
<h3 id="Example">A simple command-window example</h3>
<p>Since we are building on the series of <i><b>uifigure</b></i> customizations, I expect you have already read the preceding related <i>Undocumented Matlab</i> posts:</p>
<ol>
<li><a href="https://undocumentedmatlab.com/articles/customizing-uifigures-part-1" target="_blank">Customizing uifigures part 1</a></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-uifigures-part-2" target="_blank">Customizing uifigures part 2</a></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-uifigures-part-3" target="_blank">Customizing uifigures part 3</a></li>
</ol>
<p>Also, I <i>highly recommend</i> cloning (or at least downloading) the <a href="https://github.com/StackOverflowMATLABchat/mlapptools" rel="nofollow" target="_blank">mlapptools toolbox</a> repo on Github (thanks <a href="http://bcukurel.net.technion.ac.il" rel="nofollow" target="_blank">Iliya Romm</a> et al.). We will use it to simplify life today.<br />
<!--
Alright, let's say your GUI calls for a scroll-panel full of knobs, because who doesn't love having a plethora of knobs around?
<center><img decoding="async" src="https://undocumentedmatlab.com/images/uipanel_scrollApp.gif" alt="A scrollable panel can hide control widgets easily!" title="A scrollable panel can hide control widgets easily!" width="80%" style="max-width:822px;" /></center>
--><br />
Using the <code>mlapptools</code> toolbox, we need just a few lines of code to set up a scrollable panel. The important thing is knowing how big the panel needs to be to hold all of our control objects. Once we know this, we simply set the panel&#8217;s <b>Position</b> property accordingly. Then we can use simple CSS to display scrollbars and define the viewing dimensions.<br />
For example, if we need 260px in width by 720px in height to hold our control widgets, but only have 200px height available, we can solve this problem in 3 steps:</p>
<ol>
<li>Set the <i><b>uipanel</b></i>&#8216;s <b>Dimension</b> property to be 260px wide by 720px tall.</li>
<li>Set the viewing height using <code>mlapptools.setStyle(scrollPane,'height','200px')</code> for the panel&#8217;s CSS <code>height</code> style attribute.</li>
<li>Display the vertical scrollbar by calling <code>mlapptools.setStyle(scrollPane,'overflow-y','scroll')</code> for the panel&#8217;s CSS <code>overflow-y='scroll'</code> style attribute.</li>
</ol>
<pre lang="matlab">
% Create the figure
fig = uifigure('Name', 'Scroll Panel Test');
% Place a uipanel on the figure, color it for fun
scrollPane = uipanel(fig, 'BackgroundColor', [0.5,0.4,1]);
% Define the space requirements for the controls
totalWidth  = 260; %px
totalHeight = 720; %px
viewHeight  = 200; %px
% STEP 1: set the uipanel's Position property to the required dimensions
scrollPane.Position(3) = totalWidth;  % WIDTH
scrollPane.Position(4) = totalHeight; % HEIGHT
% STEP 2: set the viewing height
mlapptools.setStyle(scrollPane, 'height', sprintf('%dpx', viewHeight));
% STEP 3: display the vertical scrollbar
mlapptools.setStyle(scrollPane, 'overflow-y', 'scroll');
% Add control elements into the uipanel
...
</pre>
<p><center style="font-size:12px;"><img decoding="async" src="https://undocumentedmatlab.com/images/uipanel_exampleFig.png" alt="Example scrollable uipanel in a Matlab uifigure" title="Example scrollable uipanel in a Matlab uifigure" width="70%" style="max-width:450px;"/><!-- 619px --><br />
Example scrollable uipanel in a Matlab uifigure</center><br />
Because this is a web-based GUI, notice that you can simply hover your mouse over the panel and scroll with your scroll wheel. Awesome, right?<br />
Note that the CSS <code>height</code>/<code>width</code> style attributes don&#8217;t affect the actual size of our panel, just how much of the panel we can see at once (&#8220;viewport&#8221;).<br />
The CSS <code>overflow</code> style attribute has a number of options. For example, <code>setStyle(scrollPane,'overflow','auto')</code> causes scrollbars to automatically hide when the viewing area is larger than panel&#8217;s dimensions. Review the <a href="https://www.w3schools.com/css/css_overflow.asp" rel="nofollow" target="_blank">CSS <code>overflow</code> attribute</a> to learn about other related settings that affect the panel&#8217;s behavior.</p>
<h3 id="Scrollbars">Styling the scrollbars</h3>
<p>The <code>mlapptools</code> toolbox utilizes <i>dojo.js</i> to query the DOM and set styles, which is essentially setting inline styles on the DOM element, i.e. <code>&lt;div ... style="color: red;background:#FEFEFE;..."&gt; ... &lt;/div&gt;</code>. This has the added benefit of overriding any CSS classes Matlab is imposing on the DOM (see <a href="https://css-tricks.com/precedence-css-order-css-matters" rel="nofollow" target="_blank">CSS precedence</a>). We can inject our own classes into the page&#8217;s <code>&lt;head&gt;</code> tag and then use <code>dojo.setClass()</code> to apply our classes to specific GUI elements. For example, we can style our bland scrollbars by using CSS to define a custom <code>scrollpane</code> style class:</p>
<pre lang="css">
/* CSS To stylize the scrollbar */
.scrollpane::-webkit-scrollbar {
  width: 20px;
}
/* Track */
.scrollpane::-webkit-scrollbar-track {
  background-color: white;
  box-shadow: inset 0 0 5px white;
  border-radius: 10px;
}
/* Handle */
.scrollpane::-webkit-scrollbar-thumb {
  background: red;
  border-radius: 10px;
}
/* Handle on hover */
.scrollpane::-webkit-scrollbar-thumb:hover {
  background: #b30000;
}
</pre>
<p>To get the CSS into the document header, we need to first convert (&#8220;stringify&#8221;) it in Matlab:</p>
<pre lang="matlab">
% CSS styles 'stringified' for MATLAB
%  note that the whole style tag is wrapped in single quotes, that is required!
%  i.e. '<style>...</style>' which prints to the command window as:
%   ''<style>...</style>''
cssText = [...
  '''<style>\n', ...
  '  .scrollpane::-webkit-scrollbar {\n', ...
  '    width: 20px;\n', ...
  '  }\n', ...
  '  /* Track */\n', ...
  '  .scrollpane::-webkit-scrollbar-track {\n', ...
  '    background-color: white;\n', ...
  '    box-shadow: inset 0 0 5px white;\n', ...
  '    border-radius: 10px;\n', ...
  '  }\n', ...
  '  /* Handle */\n', ...
  '  .scrollpane::-webkit-scrollbar-thumb {\n', ...
  '    background: red; \n', ...
  '    border-radius: 10px;\n', ...
  '  }\n', ...
  '  /* Handle on hover */\n', ...
  '  .scrollpane::-webkit-scrollbar-thumb:hover {\n', ...
  '    background: #b30000; \n', ...
  '  }\n', ...
  '</style>''' ...
  ];
</pre>
<p>As explained in <a href="https://undocumentedmatlab.com/articles/customizing-uifigures-part-1" target="_blank">Customizing uifigures part 1</a>, we can execute JavaScript (JS) commands through the <code>webWindow</code> object. To simplify it, we use the method from <a href="https://undocumentedmatlab.com/articles/customizing-uifigures-part-2" target="_blank">Customizing uifigures part 2</a> to obtain the <code>webWindow</code> object for our GUI window and and use the <code>executeJS()</code> method to add our HTML into the document&#8217;s <code>&lt;head&gt;</code> tag. It is worth checking out the properties and methods of <a href="https://www.w3schools.com/jsref/dom_obj_document.asp" rel="nofollow" target="_blank">the JS <code>document</code> object</a>.</p>
<pre lang="matlab">
% get the webWindow object
webWindow = mlapptools.getWebWindow(fig);
% inject the CSS
webWindow.executeJS(['document.head.innerHTML += ',cssText]);
</pre>
<p>Now the tricky part is that we have to assign our new CSS <code>scrollpane</code> class to our <i><b>uipanel</b></i>. We need 2 things: the <code>webWindow</code> object and the <code>data-tag</code> (our panel&#8217;s unique ID) attribute.</p>
<pre lang="matlab">
% get the uipanel data-tag attr.
[webWindow, panelID] = mlapptools.getWebElements(scrollPane);
%make a dojo.js statement (use addClass method)
setClassString = sprintf(...
  'dojo.addClass(dojo.query("[%s = ''%s'']")[0], "%s")',...
  panelID.ID_attr, panelID.ID_val, 'scrollpane');
% add class to DOM element
webWindow.executeJS(setClassString);
</pre>
<p><center style="font-size:12px;"><img decoding="async" src="https://undocumentedmatlab.com/images/uipanel_styledScroll.gif" alt="The results of applying our scrollpane CSS style on our scroll-pane's scrollbars" title="The results of applying our scrollpane CSS style on our scroll-pane's scrollbars" width="70%" style="max-width:450px;"/><!-- 566px --><br />
The results of applying our <code>scrollpane</code> CSS style on our scroll-pane&#8217;s scrollbars</center><br />
Note: Unfortunately, because of CSS precedence rules, we may have to use the dreaded <a href="https://www.smashingmagazine.com/2010/11/the-important-css-declaration-how-and-when-to-use-it" rel="nofollow" target="_blank"><code>!important</code> CSS qualifier</a> to get the desired effect. So if the result doesn&#8217;t match your expectations, try adding <code>!important</code> to the CSS class attributes.</p>
<h3 id="uipanel">Styling the <i>uipanel</i></h3>
<p>Each <i><b>uipanel</b></i> appears to be composed of 4 <code>&lt;div&gt;</code> HTML elements: a wrapper, internal container for the panel title, a separator, and the panel&#8217;s body (contents). We first use <code>mlapptools.getWebElements()</code> to get the <code>data-tag</code> ID for the wrapper node. We can then apply styles to the wrapper, or any child node, with CSS and JS.<br />
For example, we can use CSS3 patterns (such as one in <a href="http://lea.verou.me/css3patterns/#" rel="nofollow" target="_blank">this CSS3 gallery</a>) to liven up our panel. We can also use CSS to define a block element that will replace the title container with some static text. The CSS below would be appended to the <code>cssText</code> string we made for styling the scrollbars above:</p>
<pre lang="css">
/* DECORATE THE BACKGROUND */
/* Stars modified from: http://lea.verou.me/css3patterns/#stars */
.scrollpane {
  overflow: auto;
  background:
  linear-gradient(324deg, #232927 4%,   transparent 4%)   -70px 43px,
  linear-gradient( 36deg, #232927 4%,   transparent 4%)    30px 43px,
  linear-gradient( 72deg, #e3d7bf 8.5%, transparent 8.5%)  30px 43px,
  linear-gradient(288deg, #e3d7bf 8.5%, transparent 8.5%) -70px 43px,
  linear-gradient(216deg, #e3d7bf 7.5%, transparent 7.5%) -70px 23px,
  linear-gradient(144deg, #e3d7bf 7.5%, transparent 7.5%)  30px 23px,
  linear-gradient(324deg, #232927 4%,   transparent 4%)   -20px 93px,
  linear-gradient( 36deg, #232927 4%,   transparent 4%)    80px 93px,
  linear-gradient( 72deg, #e3d7bf 8.5%, transparent 8.5%)  80px 93px,
  linear-gradient(288deg, #e3d7bf 8.5%, transparent 8.5%) -20px 93px,
  linear-gradient(216deg, #e3d7bf 7.5%, transparent 7.5%) -20px 73px,
  linear-gradient(144deg, #e3d7bf 7.5%, transparent 7.5%)  80px 73px !important;
  background-color: #232977 !important;
  background-size: 100px 100px !important;
}
/* STYLES TO CENTER A TEXT BLOCK ON A WHITE SEMI-TRANSPARENT BACKGROUND BLOCK */
/* White block div */
.blockdiv {
  color: black;
  font: 25px Arial, sans-serif !important;
  background: rgba(255,255,255,0.75);
  width: 100%;
  height: 30px;
}
/* Text container centered in .blockdiv */
.textdiv {
  position: relative;
  float: left;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</pre>
<p>To insert a static text element and its container, we can create a small JS routine that creates parent and child nodes that replace the panel&#8217;s title container:</p>
<pre lang="matlab">
% Make a nodeID string to make the JS call more generic
nodeID = sprintf('''[%s="%s"]''', panelID.ID_attr, panelID.ID_val);
% JS that creates a div within a div, each with their own classes
% The inner div gets the text and is centered within the outer div
% These elements are added before the node MATLAB will use for any controls
% added to scrollPane
js = sprintf( ...
  [ ...
    'var d = document.createElement("div");', ...
    'var t = document.createElement("div");', ...
    'd.classList.add("blockdiv");',...
    't.classList.add("textdiv");', ...
    't.innerHTML= "Some Static Text";', ...
    'd.appendChild(t);', ...
    'document.querySelectorAll(%s)[0]',...
    '.replaceChild(d,document.querySelectorAll(%s)[0].firstChild);' ...
  ], ...
  nodeID, nodeID ...
);
% execute the JS
webWindow.executeJS(js);
</pre>
<p><center style="font-size:12px;"><img decoding="async" src="https://undocumentedmatlab.com/images/uipanel_bgScroll.gif" alt="Panel background and static elements" title="Panel background and static elements" width="70%" style="max-width:450px;"/><!-- 566px --><br />
Panel background and static elements</center><br />
It seems to me that this approach might help to make lighter-weight apps, instead of having to make all those <i><b>app.Label</b></i> objects in Matlab&#8217;s App-Designer.</p>
<h3 id="Recap">Quick recap</h3>
<p>So let&#8217;s restate the process:</p>
<ol>
<li>Create a <i><b>uipanel</b></i> with the <b>Position</b> property set accordingly large enough for your control elements.</li>
<li>Use <code>mlapptools.setStyle()</code> to set the <a href="https://www.w3schools.com/css/css_overflow.asp" rel="nofollow" target="_blank"><code>overflow</code> style attribute</a> as desired.</li>
<li>Use <code>mlapptools.setStyle()</code> to set the <code>width</code> and/or <code>height</code> style attributes to the viewing size (this is how big the viewing area of the panel needs to be in order to fit nicely in your app).</li>
<li>Add your control elements with the scrolling <i><b>uipanel</b></i> as the parent.</li>
<li>If you want some special styles, create a stylesheet and inject it into the <code>&lt;head&gt;</code> and be sure to add the class to your panel&#8217;s HTML <code>classList</code>.</li>
</ol>
<p>The order of items 2-4 are not really important. You just need to ensure that the panel is large enough (via its <b>Position</b> property) to include all your elements/controls.<br />
I really hope that one day soon MathWorks will add CSS and JS hooks to <i><b>uifigure</b></i> GUI components (perhaps as settable <b>CSS</b>/<b>JS</b> properties that accept strings?), so that Matlab users could attach their own CSS and JS customizations directly within AppDesigner, without having to go through such undocumented hoops as I&#8217;ve shown here. In Loren Shure&#8217;s latest blog post, Matlab product manager <a href="https://blogs.mathworks.com/loren/2018/07/25/the-state-of-app-building-in-matlab/#573b536f-26bc-4a14-8d6b-38ed3c88b8d6" rel="nofollow" target="_blank">Dave Garisson indicated</a> that this is indeed planned for a near-future Matlab release (at least for JS, but hopefully also for CSS):</p>
<blockquote><p>&#8220;we are also investigating ways to provide a documented solution for integrating 3rd party JavaScript components in MATLAB apps.&#8221;</p></blockquote>
<h3 id="Example">A complete working example</h3>
<p>I created a complete working example in Matlab&#8217;s App Designer while figuring this whole thing out. The code (<i>CWE.m</i>) can be <a href="https://github.com/Khlick/matlabUiHacks/blob/master/scrollingUIPanel/CWE.m" rel="nofollow" target="_blank">downloaded here</a>, and then run directly from Matlab&#8217;s command window. Alternatively, the corresponding App Designer file (<i>CWE.mlapp</i>) can be <a href="https://github.com/Khlick/matlabUiHacks/blob/master/scrollingUIPanel/CWE.mlapp" rel="nofollow" target="_blank">downloaded here</a>. You are welcome to use/adapt the code in your own project. Just to be clear, I love wild colors and crazy themes, but I don&#8217;t recommend going this overboard for a real project.<br />
<center style="font-size:12px;"><img decoding="async" src="https://undocumentedmatlab.com/images/uipanel_ADfig_1.png" alt="Running app demo" title="Running app demo" width="80%" style="max-width:804px;"/><br />
Running app demo</center><br />
<!--


<h3 id="hijack">Hijacking the uipanel altogether</h3>


In the previous example, we replaced the <code>&lt;div&gt;</code> container for the title part of the <i><b>uipanel</b></i>. We can insert other types of DOM nodes into the <i><b>uipanel</b></i> as well, for example a JS-generated table.
Every <i><b>uifigure</b></i> is essentially a web page running within a browser. Using JS, we can query the <a href="https://www.w3schools.com/js/js_window_navigator.asp" rel="nofollow" target="_blank"><code>navigator</code> object</a>, which contains a lot of information on the browser (<a href="http://help.dottoro.com/ljcsokrr.php" rel="nofollow" target="_blank">see here</a>). These <code>navigator</code> properties can be displayed in a JS-generated table within a Matlab <i><b>uipanel</b></i> (<a href="https://undocumentedmatlab.com/files/browserInfo.m" target="_blank">downloadable source code</a>):
<center style="font-size:12px;"><img decoding="async" src="https://undocumentedmatlab.com/images/uipanel_matlabBrowserInfo.gif" alt="HTML table built with JS from the navigator object" title="HTML table built with JS from the navigator object" width="80%" style="max-width:700px;"/>
HTML table built with JS from the navigator object</center>
Note that these <code>navigator</code> properties correspond to my specific Matlab R2018a installation:


<pre lang="matlab">
>> version %MATLAB
    '9.4.0.813654 (R2018a)'
>> mlapptools.aboutJSLibs()  % display the loaded JS
  struct with fields:
        dojo: '1.11.2.91fa0cb'
    react_js: '0.14.7'
</pre>


--><br />
I can&#8217;t thank Yair enough for suggesting that I turn this tip into a guest post for you readers. And I want to give a huge thank you to you, the reader, for persevering all the way to the end of this post&#8230;<br />
Cheers!<br />
-Khris<br />
<b><u>Addendum September 17, 2018</u></b>: Scrolling panels in uifigures are now a fully-supported documented functionality via the new <a href="https://www.mathworks.com/help/matlab/ref/matlab.ui.container.tree.scroll.html" rel="nofollow" target="_blank"><i><b>scroll</b></i></a> function and <a href="https://www.mathworks.com/help/matlab/ref/matlab.ui.figureappd-properties.html#propname_Scrollable" rel="nofollow" target="_blank"><b>Scrollable</b></a> property, starting with Matlab release R2018b. You can still use the mechanism described above, which also works for older Matlab releases.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-web-gui-uipanel">Customizing web-GUI uipanel</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/customizing-uifigures-part-3" rel="bookmark" title="Customizing uifigures part 3">Customizing uifigures part 3 </a> <small>As I have repeatedly posted in recent years, Matlab is advancing towards web-based GUI. The basic underlying technology is more-or-less stable: an HTML/Javascript webpage that is created-on-the-fly and rendered in a stripped-down browser window (based on Chromium-based jxBrowser in recent...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-uifigures-part-2" rel="bookmark" title="Customizing uifigures part 2">Customizing uifigures part 2 </a> <small>Matlab's new web-based uifigures can be customized using custom CSS and Javascript code. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-matlab-uipanels" rel="bookmark" title="Customizing Matlab uipanels">Customizing Matlab uipanels </a> <small>Matlab uipanel controls can be customized using Java in ways that are impossible with plain Matlab. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-axes-part-3-backdrop" rel="bookmark" title="Customizing axes part 3 &#8211; Backdrop">Customizing axes part 3 &#8211; Backdrop </a> <small>Matlab HG2 axes can be customized in many different ways. This article explains some of the undocumented aspects. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/customizing-web-gui-uipanel/feed</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Scrollable GUI panels</title>
		<link>https://undocumentedmatlab.com/articles/scrollable-gui-panels?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=scrollable-gui-panels</link>
					<comments>https://undocumentedmatlab.com/articles/scrollable-gui-panels#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 25 Jul 2018 09:50:31 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[uipanel]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7824</guid>

					<description><![CDATA[<p>Scrollbars can easily be added to Matlab panels, to enable scroll-panes of GUI controls and axes. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/scrollable-gui-panels">Scrollable GUI panels</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/tab-panels-uitab-and-relatives" rel="bookmark" title="Tab panels &#8211; uitab and relatives">Tab panels &#8211; uitab and relatives </a> <small>This article describes several undocumented Matlab functions that support tab-panels...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/syntax-highlighted-labels-panels" rel="bookmark" title="Syntax highlighted labels &amp; panels">Syntax highlighted labels &amp; panels </a> <small>Syntax-highlighted labels and edit-boxes can easily be displayed in Matlab GUI - this article explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-matlab-uipanels" rel="bookmark" title="Customizing Matlab uipanels">Customizing Matlab uipanels </a> <small>Matlab uipanel controls can be customized using Java in ways that are impossible with plain Matlab. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/transparent-uipanels" rel="bookmark" title="Transparent uipanels">Transparent uipanels </a> <small>Matlab uipanels can be made transparent, for very useful effects. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Matlab enables two types of GUI container types, via the <b>Units</b> property: fixed-size (<code>'pixels'</code>, <code>'chars'</code>, etc.) and flexible (<code>'normalized'</code>). In many cases, we need something in between: a panel that expands dynamically when its container grows (i.e., flexible/<code>normalized</code>), and displays scroll-bars when the container shrinks (i.e., fixed size, with a scrollable viewport). This functionality is relatively easy to achieve using a bit of undocumented magic powder. Today&#8217;s post will show how to do this with legacy (Java-based) figures, and next week&#8217;s post will do the same for web-based (JavaScript) uifigures.<br />
<center style="font-size:12px;"><img decoding="async" src="https://undocumentedmatlab.com/images/uipanel_animated.gif" alt="Scrollable Matlab GUI panel" title="Scrollable Matlab GUI panel" width="315" height="315" /><br />
Scrollable Matlab GUI panel</center><span id="more-7824"></span></p>
<h3 id="Description">Technical description</h3>
<p>The basic idea is that in HG2 (Matlab release R2014b onward), <i><b>uipanel</b></i>s are implemented using standard Java <code>JPanel</code> components. This enables all sorts of <a href="https://undocumentedmatlab.com/articles/customizing-matlab-uipanels" target="_blank">interesting customizations</a>. For the purposes of today&#8217;s discussion, the important thing to note is that the underlying <code>JPanel</code> object can be re-parented to reside inside a Java <a href="https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html" rel="nofollow" target="_blank">JScrollPanel</a>.<br />
So, the idea is to get the Matlab panel&#8217;s underlying <code>JPanel</code> object reference, then embed it within a new <code>JScrollPanel</code> object that is placed at the exact same GUI coordinates as the original panel. The essential Matlab code snippet is this:</p>
<pre lang="matlab">
% Create the Matlab uipanel in the GUI
hPanel = uipanel(...); drawnow
% Get the panel's underlying JPanel object reference
jPanel = hPanel.JavaFrame.getGUIDEView.getParent;
% Embed the JPanel within a new JScrollPanel object
jScrollPanel = javaObjectEDT(javax.swing.JScrollPane(jPanel));
% Remove the JScrollPane border-line
jScrollPanel.setBorder([]);
% Place the JScrollPanel in same GUI location as the original panel
pixelpos = getpixelposition(hPanel);
hParent = hPanel.Parent;
[hjScrollPanel, hScrollPanel] = javacomponent(jScrollPanel, pixelpos, hParent);
hScrollPanel.Units = 'norm';
% Ensure that the scroll-panel and contained panel have linked visibility
hLink = linkprop([hPanel,hScrollPanel],'Visible');
setappdata(hPanel,'ScrollPanelVisibilityLink',hLink);
</pre>
<p>Note that this code will only work with panels created in legacy figures, not web-based uifigures (as I mentioned above, a similar solution for uifigures will be presented here next week).<br />
Also note that the new scroll-panel is created with <i><b>javaObjectEDT</b></i>, in order to avoid <a href="https://undocumentedmatlab.com/articles/matlab-and-the-event-dispatch-thread-edt" target="_blank">EDT synchronization problems</a><br />
We also want to link the visibility of the scroll-panel and its contained Matlab panel (<code>hPanel</code>), so that when the panel is set to be non-visible (<code>hPanel.Visible='off'</code>), the entire scroll-panel (scrollbars included) will become invisible, and vice-versa. We can do this by linking the <b>Visible</b> property of the Matlab panel and the scroll-panel container (<code>hScrollPanel</code>) using the <i><b>linkprop</b></i> function at the bottom of the script above. Note that we must persist the resulting <code>hLink</code> otherwise it becomes defunct &#8211; this is done by using <i><b>setappdata</b></i> to store the link in the panel (this way, when the panel is deleted, so does the link).</p>
<h3 id="Resize">Resizing the container</h3>
<p>The scroll-panel is created with a specific <code>pixelpos</code> location and size, and then its container is made to have <code>normalized</code> units. This ensures that when the container (<code>hParent</code>) grows, the scroll-panel grows as well, and no scrollbars appear (since they are not needed). But when the container shrinks in the X and/or Y direction, corresponding scrollbars appear as-needed. It sounds complicated, but it&#8217;s actually very intuitive, as the animated image above shows.<br />
When the container resizes, the displayed viewport image may &#8220;jump&#8221; sideways. To fix this we can attach a simple repaint callback function to the scroll-panel&#8217;s <b>SizeChangedFcn</b> property:</p>
<pre lang="matlab">
% Attach a repaint callback function
hScrollPanel.SizeChangedFcn = @repaintScrollPane;
% Define the callback function:
function repaintScrollPane(hScrollPanel, varargin)
    drawnow
    jScrollPanel = hScrollPanel.JavaPeer;
    offsetX = 0; %or: jScrollPanel.getHorizontalScrollBar.getValue;
    offsetY = 0; %or: jScrollPanel.getVerticalScrollBar.getValue;
    jOffsetPoint = java.awt.Point(offsetX, offsetY);
    jViewport = jScrollPanel.getViewport;
    jViewport.setViewPosition(jOffsetPoint);
    jScrollPanel.repaint;
end
</pre>
<p><center><figure style="width: 315px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/uipanel4b_cropped.gif" alt="Scrollbars automatically appear as-needed during resize" title="Scrollbars automatically appear as-needed during resize" width="315" height="183" /><figcaption class="wp-caption-text">Scrollbars automatically appear as-needed during resize</figcaption></figure></center></p>
<h3 id="ViewOffset">Viewport position/offset</h3>
<p>It would be convenient to have an easy-to-use <b>ViewOffset</b> property in the <code>hScrollPanel</code> object, in order to be able to programmatically query and set the current viewport position (i.e., scrollbars offset). We can add this property via the <i><b>addprop</b></i> function:</p>
<pre lang="matlab">
% Add a new Viewoffset property to hSCrollPanel object
hProp = addprop(hScrollPanel, 'ViewOffset');
hProp.GetMethod = @getViewOffset;  %viewOffset = getViewOffset(hScrollPanel)
hProp.SetMethod = @setViewOffset;  %setViewOffset(hScrollPanel, viewOffset)
% Getter method for the dynamic ViewOffset property
function viewOffset = getViewOffset(hScrollPanel, varargin)
    jScrollPanel = hScrollPanel.JavaPeer;
    jPoint = jScrollPanel.getViewport.getViewPosition;
    viewOffset = [jPoint.getX, jPoint.getY];
end
% Setter method for the dynamic ViewOffset property
function setViewOffset(hScrollPanel, viewOffset)
    jPoint = java.awt.Point(viewOffset(1), viewOffset(2));
    jScrollPanel = hScrollPanel.JavaPeer;
    jScrollPanel.getViewport.setViewPosition(jPoint);
    jScrollPanel.repaint;
end
</pre>
<p>This enables us to both query and update the scroll-panel&#8217;s view position &#8211; <code>[0,0]</code> means top-left corner (i.e., no scroll); <code>[12,34]</code> mean scrolling 12 to the right and 34 down:</p>
<pre lang="matlab">
>> offset = hScrollPanel.ViewOffset   % or: get(hScrollPanel,'ViewOffset')
offset =
     0     0
>> offset = hScrollPanel.ViewOffset   % or: get(hScrollPanel,'ViewOffset')
offset =
    12    34
% Scroll 30 pixels right, 50 pixels down
>> hScrollPanel.ViewOffset = [30,50];   % or: set(hScrollPanel,'ViewOffset',[30,50])
</pre>
<h3 id="attachScrollPanelTo">attachScrollPanelTo utility</h3>
<p>I have prepared a utility called <i><b>attachScrollPanelTo</b></i> (<a href="https://www.mathworks.com/matlabcentral/fileexchange/68325-attachscrollpanelto-add-scroll-panel-to-a-uipanel-or-axes" rel="nofollow" target="_blank">downloadable from the Matlab File Exchange</a>), which encapsulates all of the above, plus a few other features: inputs validation, <b>Viewport</b> property in the output scroll-pane object, automatic encasing in a new panel for input object that are not already a panel, etc. Feel free to download the utility, use it in your program, and modify the source-code to fit your needs. Here are some usage examples:</p>
<pre lang="matlab">
attachScrollPanelTo();  % display the demo
attachScrollPanelTo(hPanel) % place the specified hPanel 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
</pre>
<p>If you&#8217;d like me to add flare to your Matlab GUI, don&#8217;t hesitate to contact me on my <a href="https://undocumentedmatlab.com/consulting" target="_blank">Consulting page</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/scrollable-gui-panels">Scrollable GUI panels</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/tab-panels-uitab-and-relatives" rel="bookmark" title="Tab panels &#8211; uitab and relatives">Tab panels &#8211; uitab and relatives </a> <small>This article describes several undocumented Matlab functions that support tab-panels...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/syntax-highlighted-labels-panels" rel="bookmark" title="Syntax highlighted labels &amp; panels">Syntax highlighted labels &amp; panels </a> <small>Syntax-highlighted labels and edit-boxes can easily be displayed in Matlab GUI - this article explains how. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-matlab-uipanels" rel="bookmark" title="Customizing Matlab uipanels">Customizing Matlab uipanels </a> <small>Matlab uipanel controls can be customized using Java in ways that are impossible with plain Matlab. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/transparent-uipanels" rel="bookmark" title="Transparent uipanels">Transparent uipanels </a> <small>Matlab uipanels can be made transparent, for very useful effects. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/scrollable-gui-panels/feed</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>Customizing Matlab uipanels</title>
		<link>https://undocumentedmatlab.com/articles/customizing-matlab-uipanels?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=customizing-matlab-uipanels</link>
					<comments>https://undocumentedmatlab.com/articles/customizing-matlab-uipanels#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 25 Feb 2015 12:24:50 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Hidden property]]></category>
		<category><![CDATA[Icons]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Stock Matlab function]]></category>
		<category><![CDATA[UI controls]]></category>
		<category><![CDATA[HG2]]></category>
		<category><![CDATA[uipanel]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=5595</guid>

					<description><![CDATA[<p>Matlab uipanel controls can be customized using Java in ways that are impossible with plain Matlab. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-matlab-uipanels">Customizing Matlab uipanels</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/customizing-uicontrol-border" rel="bookmark" title="Customizing uicontrol border">Customizing uicontrol border </a> <small>Matlab uicontrol borders can easily be modified - this article shows how...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-matlab-labels" rel="bookmark" title="Customizing Matlab labels">Customizing Matlab labels </a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/transparent-uipanels" rel="bookmark" title="Transparent uipanels">Transparent uipanels </a> <small>Matlab uipanels can be made transparent, for very useful effects. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-editboxes" rel="bookmark" title="Customizing editboxes">Customizing editboxes </a> <small>Matlab's editbox can be customized in many useful manners...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>The major innovation in Matlab release R2014b was the introduction of the new handle-based graphics system (<a target="_blank" href="/articles/hg2-update">HG2</a>). However, this release also included a few other improvements to graphics/GUI that should not be overlooked. The most notable is that <i><b>uitab</b></i>s are finally officially documented/supported, following a decade or being undocumented (well, undocumented in the official sense, since I took the time to document this functionality in <a target="_blank" href="/articles/tab-panels-uitab-and-relatives">this blog</a> and in my <a target="_blank" href="/books/matlab-java">Matlab-Java book</a>).<br />
A less-visible improvement occurred with <i><b>uipanel</b></i>s: Panels are very important containers when designing GUIs. They enable a visual grouping of related controls and introduce order to an otherwise complex GUI. Unfortunately, until R2014b panels were drawn at the canvas level, and did not use a standard Java Swing controls like other uicontrols. This made it impossible to customize <i><b>uipanel</b></i>s in a similar manner to other GUI uicontrols (<a target="_blank" href="/articles/button-customization">example</a>).<br />
In R2014b, <i><b>uipanel</b></i>s have finally become standard Java Swing controls, a <code>com.mathworks.hg.peer.ui.UIPanelPeer$UIPanelJPanel</code> component that extends Swing&#8217;s standard <code><a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JPanel.html">javax.swing.JPanel</a></code> and Matlab&#8217;s ubiquitous <code>com.mathworks.mwswing.MJPanel</code>. This means that we can finally customize it in various ways that are not available in plain Matlab.<br />
We start the discussion with a simple Matlab code snippet. It is deliberately simple, since I wish to demonstrate only the panel aspects:</p>
<pre lang='matlab'>
figure('Menubar','none', 'Color','w');
hPanel = uipanel('Title','Panel title', 'Units','norm', 'Pos',[.1,.1,.6,.7]);
hButton = uicontrol('String','Click!', 'Parent',hPanel);
</pre>
<p><center><figure style="width: 175px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Standard Matlab uipanel" src="https://undocumentedmatlab.com/images/uipanel1.gif" title="Standard Matlab uipanel" width="175" height="140" /><figcaption class="wp-caption-text">Standard Matlab <i><b>uipanel</b></i></figcaption></figure></center><br />
Notice the default &#8216;etchedin&#8217; panel border, which I hate (note the broken edges at the corners). Luckily, Swing includes a <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/uiswing/components/border.html">wide range of alternative borders</a> that we can use. I&#8217;ve already <a target="_blank" href="/articles/customizing-uicontrol-border">demonstrated</a> customizing Matlab uicontrols with Java borders back in 2010 (has it really been that long? wow!). In R2014b we can finally do something similar to <i><b>uipanel</b></i>s:<br />
<span id="more-5595"></span><br />
The first step is to get the <i><b>uipanel</b></i>&#8216;s underlying Java component&#8217;s reference. We can do this using my <a target="_blank" href="/articles/findjobj-find-underlying-java-object"><i><b>findjobj</b></i> utility</a>, but in the specific case of <i><b>uipanel</b></i> we are lucky to have a direct shortcut by using the panel&#8217;s undocumented hidden property <b>JavaFrame</b> and its <b>PrintableComponent</b> property:</p>
<pre lang='matlab'>
>> jPanel = hPanel.JavaFrame.getPrintableComponent
jPanel =
com.mathworks.hg.peer.ui.UIPanelPeer$UIPanelJPanel[,0,0,97x74,...]
</pre>
<p>Let&#8217;s now take a look at the <code>jPanel</code>&#8216;s border:</p>
<pre lang='matlab'>
>> jPanel.getBorder
ans =
com.mathworks.hg.peer.ui.borders.TitledBorder@25cd9b97
>> jPanel.getBorder.get
                Border: [1x1 com.mathworks.hg.peer.ui.borders.EtchedBorderWithThickness]
          BorderOpaque: 0
                 Class: [1x1 java.lang.Class]
                 Title: 'Panel title'
            TitleColor: [1x1 java.awt.Color]
             TitleFont: [1x1 java.awt.Font]
    TitleJustification: 1
         TitlePosition: 2
</pre>
<p>Ok, simple enough. Let&#8217;s replace the border&#8217;s <code>EtchedBorderWithThickness</code> with something more appealing. We start with a simple red <code><a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/border/LineBorder.html">LineBorder</a></code> having rounded corners and 1px width:</p>
<pre lang='matlab'>
jColor = java.awt.Color.red;  % or: java.awt.Color(1,0,0)
jNewBorder = javax.swing.border.LineBorder(jColor, 1, true);  % red, 1px, rounded=true
jPanel.getBorder.setBorder(jNewBorder);
jPanel.repaint;  % redraw the modified panel
</pre>
<p><center><figure style="width: 175px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Rounded-corners LineBorder" src="https://undocumentedmatlab.com/images/uipanel2.gif" title="Rounded-corners LineBorder" width="175" height="140" /><figcaption class="wp-caption-text">Rounded-corners LineBorder</figcaption></figure></center><br />
Or maybe a thicker non-rounded orange border:</p>
<pre lang='matlab'>
jColor = java.awt.Color(1,0.5,0);
jNewBorder = javax.swing.border.LineBorder(jColor, 3, false);  % orange, 3px, rounded=false
jPanel.getBorder.setBorder(jNewBorder);
jPanel.repaint;  % redraw the modified panel
</pre>
<p><center><figure style="width: 175px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Another LineBorder example" src="https://undocumentedmatlab.com/images/uipanel3.gif" title="Another LineBorder example" width="175" height="140" /><figcaption class="wp-caption-text">Another LineBorder example</figcaption></figure></center><br />
Or maybe a <code><a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/border/MatteBorder.html">MatteBorder</a></code> with colored insets:</p>
<pre lang='matlab'>
jColor = java.awt.Color(0,0.3,0.8);  % light-blue
jNewBorder = javax.swing.border.MatteBorder(2,5,8,11,jColor)  % top,left,bottom,right, color
jPanel.getBorder.setBorder(jNewBorder);
jPanel.repaint;  % redraw the modified panel
</pre>
<p><center><figure style="width: 175px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="MatteBorder with solid insets" src="https://undocumentedmatlab.com/images/uipanel4.gif" title="MatteBorder with solid insets" width="175" height="140" /><figcaption class="wp-caption-text">MatteBorder with solid insets</figcaption></figure></center><br />
<code>MatteBorder</code> can also use an icon (rather than a solid color) to fill the border insets. First, let&#8217;s load the icon. We can either load a file directly from disk, or use one of Matlab&#8217;s standard icons. Here are both of these alternatives:</p>
<pre lang='matlab'>
% Alternative #1: load from disk file
icon = javax.swing.ImageIcon('C:\Yair\star.gif');
% Alternative #2: load a Matlab resource file
jarFile = fullfile(matlabroot,'/java/jar/mlwidgets.jar');
iconsFolder = '/com/mathworks/mlwidgets/graphics/resources/';
iconURI = ['jar:file:/' jarFile '!' iconsFolder 'favorite_hoverover.png'];  % 14x14 px
icon = javax.swing.ImageIcon(java.net.URL(iconURI));
</pre>
<p>We can now pass this icon reference to <code>MatteBorder</code>&#8216;s constructor:</p>
<pre lang='matlab'>
w = icon.getIconWidth;
h = icon.getIconHeight;
jNewBorder = javax.swing.border.MatteBorder(h,w,h,w,icon)  % top,left,bottom,right, icon
jPanel.getBorder.setBorder(jNewBorder);
jPanel.repaint;  % redraw the modified panel
</pre>
<p><center><figure style="width: 175px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="MatteBorder with icon insets" src="https://undocumentedmatlab.com/images/uipanel5.gif" title="MatteBorder with icon insets" width="175" height="140" /><figcaption class="wp-caption-text">MatteBorder with icon insets</figcaption></figure></center><br />
Additional useful Swing borders can be found in the list of classes implementing the <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/border/Border.html"><code>Border</code> interface</a>, or via the <code><a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/BorderFactory.html">BorderFactory</a></code> class. For example, let&#8217;s create a <a target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/7/docs/api/javax/swing/BorderFactory.html#createDashedBorder(java.awt.Paint,%20float,%20float,%20float,%20boolean)">dashed border</a> having a 3-2 ratio between the line lengths and the spacing:</p>
<pre lang='matlab'>
jColor = java.awt.Color.blue;  % or: java.awt.Color(0,0,1);
lineWidth = 1;
relativeLineLength = 3;
relativeSpacing = 2;
isRounded = false;
jNewBorder = javax.swing.BorderFactory.createDashedBorder(jColor,lineWidth,relativeLineLength,relativeSpacing,isRounded);
jPanel.getBorder.setBorder(jNewBorder);
jPanel.repaint;  % redraw the modified panel
</pre>
<p><center><figure style="width: 175px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="StrokeBorder (dashed)" src="https://undocumentedmatlab.com/images/uipanel6.gif" title="StrokeBorder (dashed)" width="175" height="140" /><figcaption class="wp-caption-text">StrokeBorder (dashed)</figcaption></figure></center><br />
After seeing all these possibilities, I think you&#8217;ll agree with me that Matlab&#8217;s standard <i><b>uipanel</b></i> borders look pale in comparison.<br />
Have you used any interesting borders in your Matlab GUI? Or have you customized your panels in some other nifty manner? If so, then please place a comment below.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/customizing-matlab-uipanels">Customizing Matlab uipanels</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/customizing-uicontrol-border" rel="bookmark" title="Customizing uicontrol border">Customizing uicontrol border </a> <small>Matlab uicontrol borders can easily be modified - this article shows how...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-matlab-labels" rel="bookmark" title="Customizing Matlab labels">Customizing Matlab labels </a> <small>Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/transparent-uipanels" rel="bookmark" title="Transparent uipanels">Transparent uipanels </a> <small>Matlab uipanels can be made transparent, for very useful effects. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/customizing-editboxes" rel="bookmark" title="Customizing editboxes">Customizing editboxes </a> <small>Matlab's editbox can be customized in many useful manners...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/customizing-matlab-uipanels/feed</wfw:commentRss>
			<slash:comments>14</slash:comments>
		
		
			</item>
	</channel>
</rss>
