A few months ago I published a post about Matlab’s semi-documented tab-panel functionality, where I promised a follow-up article on tab customizations. A reader of this blog asked a related question earlier today, so I decided it’s about time I fulfilled this promise.
As with most Matlab controls, the underlying Java component enables far greater customization than possible using plain Matlab. Today I will show three specific customizations. We start with a basic tab group, and get the underlying Java component:
% Prevent an annoying warning msg warning off MATLAB:uitabgroup:OldVersion % Prepare a tab-group consisting of two tabs hTabGroup = uitabgroup; drawnow; tab1 = uitab(hTabGroup, 'title','Panel 1'); a = axes('parent', tab1); surf(peaks); tab2 = uitab(hTabGroup, 'title','Panel 2'); uicontrol(tab2, 'String','Close', 'Callback','close(gcbf)'); % Get the underlying Java reference (use hidden property) jTabGroup = getappdata(handle(hTabGroup),'JTabbedPane');
Foreground & background tab colors
We can set the tab font color using setForeground() and setForegroundAt(), or via HTML. Note that setForegroundAt() overrides anything set by setForeground(). Also remember that Java uses 0-based indexing so tab #1 is actually the second tab:
% Equivalent manners to set a red tab foreground: jTabGroup.setForegroundAt(1,java.awt.Color(1.0,0,0)); % tab #1 jTabGroup.setTitleAt(1,'<html><font color="red"><i>Panel 2'); jTabGroup.setForeground(java.awt.Color.red);
Unfortunately, the corresponding setBackgroundAt(tabIndex,color) method has no visible effect, and the Matlab-extended tabs keep their white/gray backgrounds. A similar attempt to modify the tab’s BackgroundColor property fails, since Matlab made this property unmodifiable (=’none’). A simple solution is to use a CSS background:
% Equivalent manners to set a yellow tab background: jTabGroup.setTitleAt(0,'<html><div style="background:#ffff00;">Panel 1'); jTabGroup.setTitleAt(0,'<html><div style="background:yellow;">Panel 1');
We can set the foreground text color using the CSS color directive. Similarly, we can also set a background gradient image for the tabs, using the CSS background-image directive. Which leads us to our next customization:
Icons and sub-components can be added to the tabs. Unfortunately, for some reason that I do not fully understand, jTabGroup.setIconAt() has no apparent effect. The solution is to set our own custom control as the requested tab, and add our icon (or other customizations) to it. Here is a simple example:
% Add an icon to tab #1 (=second tab) icon = javax.swing.ImageIcon('C:\Yair\save.gif'); jLabel = javax.swing.JLabel('Tab #2'); jLabel.setIcon(icon); jTabGroup.setTabComponentAt(1,jLabel); % Tab #1 = second tab % Note: icon is automatically grayed when label is disabled jLabel.setEnabled(false); jTabGroup.setEnabledAt(1,false); % disable only tab #1
Now let’s try a more complex example, of adding a close (‘x’) button to one of the tabs. Generalizing this code snippet is left as an exercise to the reader:
% First let's load the close icon jarFile = fullfile(matlabroot,'/java/jar/mwt.jar'); iconsFolder = '/com/mathworks/mwt/resources/'; iconURI = ['jar:file:/' jarFile '!' iconsFolder 'closebox.gif']; icon = javax.swing.ImageIcon(java.net.URL(iconURI)); % Now let's prepare the close button: icon, size and callback jCloseButton = handle(javax.swing.JButton,'CallbackProperties'); jCloseButton.setIcon(icon); jCloseButton.setPreferredSize(java.awt.Dimension(15,15)); jCloseButton.setMaximumSize(java.awt.Dimension(15,15)); jCloseButton.setSize(java.awt.Dimension(15,15)); set(jCloseButton, 'ActionPerformedCallback',@(h,e)delete(tab2)); % Now let's prepare a tab panel with our label and close button jPanel = javax.swing.JPanel; % default layout = FlowLayout set(jPanel.getLayout, 'Hgap',0, 'Vgap',0); % default gap = 5px jLabel = javax.swing.JLabel('Tab #2'); jPanel.add(jLabel); jPanel.add(jCloseButton); % Now attach this tab panel as the tab-group's 2nd component jTabGroup.setTabComponentAt(1,jPanel); % Tab #1 = second tab
Next week’s article will conclude the series on Matlab’s uitab. Any particular customization you are interested in? Please do post a comment.
Addendum Oct 3 2014: the uitab and uitabgroup functions have finally become fully supported and documented in Matlab version 8.4 (R2014b). However, the Java-based customizations shown in this article are still unsupported and undocumented, although they remain practically unchanged from what I’ve described in this article, four years earlier.
- Uitab customizations This article shows several customizations that can be done to Matlab's undocumented tab-panels functionality...
- Tab panels – uitab and relatives This article describes several undocumented Matlab functions that support tab-panels...
- Setting system tray icons System-tray icons can be programmatically set and controlled from within Matlab, using new functionality available since R2007b....
- Images in Matlab uicontrols & labels Images can be added to Matlab controls and labels in a variety of manners, documented and undocumented. ...
- Uitable cell colors A few Java-based customizations can transform a plain-looking data table into a lively colored one. ...
- Changing Matlab’s Command Window colors Matlab's Command Window foreground and background colors can be modified programmatically, using some of Matlab's undocumented internal Java classes. Here's how....