I’ve written here in the past about how Matlab includes multiple alternatives for color selection, plot-type selection and date selection components, that can easily be integrated in Matlab figures (GUI). Today, I will show that Matlab also contains various built-in components for font selection.
These components are used by Matlab itself, integrated within the Preferences panel, print setup popup, property inspector window and so on. In most cases the components have remained unchanged for multiple releases, some existing in Matlab releases for the past decade or more. However, since internal components can change without prior notice, there is no assurance that any particular component will continue to be available in future Matlab releases.
Readers who are interested in additional details about the components mentioned in today’s post are referred to sections 3.3.3 and 5.5.2 of my book, Undocumented Secrets of MATLAB-Java Programming.
The only documented font-selection alternative in Matlab is uisetfont, which presents a popup dialog window that returns the selected font properties in a simple Matlab struct:
>> font = uisetfont font = FontName: 'Arial' FontWeight: 'normal' FontAngle: 'normal' FontUnits: 'points' FontSize: 10
The main drawback of uisetfont is the fact that it displays a separate non-resizable modal dialog window. We cannot embed uisetfont within our own panel, integrated in our GUI figure.
DesktopFontPicker is a Swing component that presents a font selection panel that can easily be inserted into any Matlab GUI container (figure, panel or tab) using the javacomponent function:
font = java.awt.Font('Tahoma',java.awt.Font.PLAIN, 11); jFontPanel = com.mathworks.widgets.DesktopFontPicker(true, font); [jhPanel,hContainer] = javacomponent(jFontPanel, [10,10,250,170], gcf);
Instead of the “Use desktop font” label, we can use our own label:
jFontPanel.setUseDesktopFontLabel('Use Yair''s standard font...')
To extract the selected font, use one of the following methods provided by
jFont = jFontPanel.getSelectedFont(); % returns a java.awt.Font object flag = jFontPanel.getUseDesktopFont(); % true if top radio-button is selected; false if custom font is selected
com.mathworks.mlwidgets.prefs.FontPrefsPanel class is used in Matlab to display the font preferences panel in the Preferences window. We can integrate it directly in our GUI:
jFontPanel=com.mathworks.mlwidgets.prefs.FontPrefsPanel(java.awt.Dimension); [jhPanel,hContainer] = javacomponent(jFontPanel, [1,1,500,470], gcf);
Using this class is admittedly more cumbersome than
DesktopFontPicker and I would not recommend using it in practice.
Font selection can also be shown with drop-downs (combo-boxes), rather than with lists as in
FontPrefsPanel, or uisetfont. Use of drop-downs significantly reduces the display “real-estate” required by the control. This is useful in forms where the font selection is only one of several user-configurable options, and where enough space must be reserved for other configuration controls. We can do this using the
Up until Matlab release R2010a,
FontPicker‘s constructor accepted optional parameters of a pre-selected font (a
java.awt.Font object), an optional boolean flag indicating whether to display sample text using the selected font, an optional layout indicator, and optional list of selectable font names. Several screenshots of different parameter combinations are shown below:
import com.mathworks.widgets.fonts.FontPicker jFontPicker = FontPicker(font, sampleFlag, layout); [hjFontPicker, hContainer] = javacomponent(jFontPicker, position, gcf);
As before, the selected font can be retrieved using
In Matlab release R2010b,
FontPicker‘s interface changed, and the above code no longer works. This highlights a common pitfall in future-compatibility of internal components: even when the components remain, their interface sometimes changes. Here is the new code format, starting with R2010b:
jLayout = javaMethod('valueOf', 'com.mathworks.widgets.fonts.FontPicker$Layout', 'WIDE_WITH_SAMPLE'); % options: COMPACT, WIDE, WIDE_WITH_SAMPLE jFont = java.awt.Font('Tahoma', java.awt.Font.PLAIN, 10); % initial font to display (may not be ) jFontPicker = com.mathworks.widgets.fonts.FontPicker(jFont, jLayout); jFontPanel = jFontPicker.getComponent; [jhPanel,hContainer] = javacomponent(jFontPanel, [10,10,250,120], gcf);
As a final alternative for font selection, we can use the JIDE font-selection component. This component has two variants: as a drop-down/combo-box (
com.jidesoft.combobox.FontComboBox) and as a standard
jFont = java.awt.Font('arial black',java.awt.Font.PLAIN, 8); jFontPicker = com.jidesoft.combobox.FontComboBox(jFont); [hjFontPicker, hContainer] = javacomponent(jFontPicker, position, gcf); set(hjFontPicker, 'ItemStateChangedCallback', @myCallbackFunction);
Within the callback function, use getSelectedFont() to retrieve the updated font (again, a
java.awt.Fontobject). There is also a corresponding setSelectedFont(font) to programmatically update the control with the specified
The combo-box presents a
FontChooserPanel, which can be accessed (via the PopupPanel property or the corresponding getPopupPanel() method) after it has been initially created. Thereafter, the panel can be customized. For example, the preview text can be modified via the panel’s PreviewText property (or the setPreviewText(text) method).
FontChooserPanelcan also be displayed as a stand-alone font-selection panel, unrelated to any combo-box. Different GUI requirements might prefer using a compact combo-box approach, or the larger stand-alone panel.
This combo-box/panel duality is a common feature of JIDE controls. I have previously shown it in my color selection components and date selection components articles.
As another example of using a font-selection drop-down (combo-box), we can use a standard Matlab popupmenu uicontrol, setting its String property value to a cell-array containing the supported system’s fonts (as returned by the listfonts function). A nice twist here is to use the undocumented trick that all Matlab uicontrols inherently support HTML to list each of the fonts in their respective font style:
fontStr = @(font) ['<html><font face="' font '">' font '</font></html>']; htmlStr = cellfun(fontStr, listfonts, 'uniform',false); uicontrol('style','popupmenu', 'string',htmlStr, 'pos',[20,350,100,20]);
Note that we could also use a listbox uicontrol using the same code.
Austria visit, 11-15 October, 2015
I will be travelling to clients in Austria next week, between October 11-15. If you are in Austria and wish to meet me to discuss how I could bring value to your work, then please email me (altmany at gmail).