- Undocumented Matlab - https://undocumentedmatlab.com -
Matlab toolstrip – part 5 (icons)
Posted By Yair Altman On January 6, 2019 | 2 Comments
In a previous post [1] I showed how we can create custom Matlab app toolstrips. Toolstrips can be a bit complex to develop so I’m trying to proceed slowly, with each post in the miniseries building on the previous posts. I encourage you to review the earlier posts in the Toolstrip miniseries [2] before reading this post. Today’s post describes how we can set various icons, based on the toolstrip created in the previous posts:
Many toolstrip controls (such as buttons, but not checkboxes for example) have a settable Icon property. The standard practice is to use a 16×16 icon for a component within a multi-component toolstrip column (i.e., when 2 or 3 components are displayed on top of each other), and a 24×24 icon for a component that spans the entire column height (i.e., when the column contains only a single component).
We can use one of the following methods to specify the icon. Note that you need to import matlab.ui.internal.toolstrip.*
if you wish to use the Icon
class without the preceding package name.
[]
) by default, meaning that no icon is displayed.Icon.<icon-name>
. For example: icon = Icon.REFRESH_24
. These icons typically come in 2 sizes: 16×16 pixels (e.g. Icon.REFRESH_16) that we can use with the small-size components (which are displayed when the column has 2-3 controls), and 24×24 pixels (e.g. REFRESH_24) that we can use with the large-size components (which are displayed when the column contains only a single control). You can see the list of the standard icons by running
matlab.ui.internal.toolstrip.Icon.showStandardIcons
Icon
constructor by specifying the full filepath for any PNG or JPG image file. Note that other file type (such as GIF) are not supported by this method. For example:
icon = Icon(fullfile(matlabroot,'toolbox','matlab','icons','tool_colorbar.png')); % PNG/JPG image file (not GIF!)
In fact, the ~150 standard icons above use this mechanism under the hood: Icon.REFRESH_24
is basically a public static method of the Icon
class, which simply calls Icon('REFRESH_24','Refresh_24')
(note the undocumented use of a 2-input Icon
constructor). This method in turn uses the Refresh_24.png file in Matlab’s standard toolstrip resources folder: %matlabroot%/toolbox/shared/controllib/general/resources/toolstrip_icons/Refresh_24.png.
Icon
constructor by specifying a PNG or JPG file contained within a JAR file, using the standard jar:file:...jar!/
notation. There are numerous icons included in Matlab’s JAR files – simply open these files in WinZip or WinRar and browse. In addition, you can include images included in any external JAR file. For example:
icon = Icon(['jar:file:/' matlabroot '/java/jar/mlwidgets.jar!/com/mathworks/mlwidgets/actionbrowser/resources/uparrow.png']);
Icon
constructor by specifying a Java javax.swing.ImageIcon
object. Fortunately we can create such objects from a variety of image formats (including GIFs). For example:
iconFilename = fullfile(matlabroot,'toolbox','matlab','icons','boardicon.gif');
jIcon = javax.swing.ImageIcon(iconFilename); % Java ImageIcon from file (inc. GIF)
icon = Icon(jIcon);
If we need to resize the Java image (for example, from 16×16 to 24×24 or vise versa), we can use the following method:
% Resize icon to 24x24 pixels
jIcon = javax.swing.ImageIcon(iconFilename); % get Java ImageIcon from file (inc. GIF)
jIcon = javax.swing.ImageIcon(jIcon.getImage.getScaledInstance(24,24,jIcon.getImage.SCALE_SMOOTH)) % resize to 24x24
icon = Icon(jIcon);
The app window’s icon can also be set. By default, the window uses the standard Matlab membrane icon (%matlabroot%/toolbox/matlab/icons/matlabicon.gif). This can be modified using the hToolGroup.setIcon
method, which currently [R2018b] expects a Java ImageIcon
object as input. For example:
iconFilename = fullfile(matlabroot,'toolbox','matlab','icons','reficon.gif');
jIcon = javax.swing.ImageIcon(iconFilename);
hToolGroup.setIcon(jIcon)
This icon should be set before the toolgroup window is shown (hToolGroup.open
).
% Resize icon to 16x16 pixels
jIcon = javax.swing.ImageIcon(iconFilename); % get Java ImageIcon from file (inc. GIF)
jIcon = javax.swing.ImageIcon(jIcon.getImage.getScaledInstance(16,16,jIcon.getImage.SCALE_SMOOTH)) % resize to 16x16
hToolGroup.setIcon(jIcon)
It’s natural to expect that hToolGroup
, which is a pure-Matlab MCOS wrapper class, would have an Icon property that accepts Icon
objects, just like for controls as described above. For some reason, this is not the case. It’s very easy to fix it though – after all, the Icon
class is little more than an MCOS wrapper class for the underlying Java ImageIcon
(not exactly, but close enough). Adapting ToolGroup
‘s code to accept an Icon
is quite easy, and I hope that MathWorks will indeed implement this in a near-term future release. I also hope that MathWorks will remove the 16×16 limitation, or automatically resize icons to 16×16, or at the very least issue a console warning when a larger icon is specified by the user. Until then, we can use the setIcon(jImageIcon)
method and take care to send it the 16×16 ImageIcon
object that it expects.
The next post will discuss complex components, including button-group, drop-down, listbox, split-button, slider, popup form, gallery etc.
Following that, my plan is to discuss toolstrip collapsibility, the ToolPack framework, docking layout, DataBrowser panel, QAB (Quick Access Bar), underlying Java controls, and adding toolstrips to figures – not necessarily in this order. Matlab toolstrips can be a bit complex, so I plan to proceed in small steps, each post building on top of its predecessors.
If you would like me to assist you in building a custom toolstrip or GUI for your Matlab program, please let me know [3].
Categories: GUI, High risk of breaking in future versions, Java, Undocumented feature
Article printed from Undocumented Matlab: https://undocumentedmatlab.com
URL to article: https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons
URLs in this post:
[1] previous post: https://undocumentedmatlab.com/blog/matlab-toolstrip-part-3-basic-customization
[2] the Toolstrip miniseries: https://undocumentedmatlab.com/blog/tag/toolstrip
[3] please let me know: https://undocumentedmatlab.com/consulting
[4] Matlab toolstrip – part 3 (basic customization) : https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization
[5] Matlab toolstrip – part 9 (popup figures) : https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures
[6] Matlab toolstrip – part 2 (ToolGroup App) : https://undocumentedmatlab.com/articles/matlab-toolstrip-part-2-toolgroup-app
[7] Matlab toolstrip – part 8 (galleries) : https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries
[8] Matlab toolstrip – part 4 (control customization) : https://undocumentedmatlab.com/articles/matlab-toolstrip-part-4-control-customization
[9] Uitab colors, icons and images : https://undocumentedmatlab.com/articles/uitab-colors-icons-images
Click here to print.
Copyright © Yair Altman - Undocumented Matlab. All rights reserved.
2 Comments To "Matlab toolstrip – part 5 (icons)"
#1 Comment By Arash Marashian On April 12, 2019 @ 19:47
Hi,
Dear Yair when I’m using PLOT_16 icon MATLAB gives me an error as below:
“
The class matlab.ui.internal.toolstrip.Icon has no Constant property or Static method named 'PLOT_16'.
”What should I do?
#2 Comment By Arash Marashian On April 12, 2019 @ 19:59
Problem Solved.
I found out that my version does not have this icon.
thanks.