- Undocumented Matlab - https://undocumentedmatlab.com -

Uitab colors, icons and images

Posted By Yair Altman On November 10, 2010 | 43 Comments

A few months ago I published a post about Matlab’s semi-documented tab-panel functionality [1], where I promised a follow-up article on tab customizations. A reader of this blog asked a related question [2] 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,'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 [3]:

% Equivalent manners to set a yellow tab background:
jTabGroup.setTitleAt(0,'
Panel 1'); jTabGroup.setTitleAt(0,'
Panel 1');

uitabgroup with non-default forground and background tab colors and fonts
uitabgroup with non-default forground and background tab colors and fonts

We can set the foreground text color using the CSS color directive [4]. 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:

Icon images

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

tab with a custom icon (enabled)
tab with a custom icon (enabled)
tab with a custom icon (enabled & disabled)

Close buttons

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

tab with an attached close button
tab with an attached close button

Next week’s article will conclude the series on Matlab’s uitab. Any particular customization you are interested in? Please do post a comment [5].
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.

Categories: GUI, Icons, Java, Medium risk of breaking in future versions, Semi-documented function


43 Comments (Open | Close)

43 Comments To "Uitab colors, icons and images"

#1 Comment By Alex On December 2, 2010 @ 08:52

If I execute the following code, the Tab with the icon is not greyed out. It is obviously disabled because I cant click on it, but it looks exactly like an enabled tab. (Icon and Label ar not grey).

% 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');

% Add an icon to tab #1 (=second tab)
icon = javax.swing.ImageIcon('Find.gif');
jLabel = javax.swing.JLabel('Tab #2');
jLabel.setIcon(icon);
jTabGroup.setTabComponentAt(1,jLabel);	% Tab #1 = second tab
 
% Note: both label & icon automatically grayed when disabled
jTabGroup.setEnabledAt(1,false);  % disable only tab #1

If I comment the paragraph titled with “% Add an icon to tab #1”, the tab is greyed out properly (without icon). You wrote that icons are automatically greyed out, but I can’t get it to work. Does this code work on your computer?

#2 Comment By Yair Altman On December 2, 2010 @ 11:26

@Alex – you are correct: the missing code line is:

jLabel.setEnabled(false);

-Yair

#3 Comment By john On January 28, 2011 @ 18:18

Hi,

Im trying to make the title of tabs bold but am having some trouble using html doing it. I tried using

jTabGroup.setForeground(java.awt.Font.BOLD);

But this doesnt seem to work, any suggestions? Thanks

#4 Comment By Yair Altman On January 29, 2011 @ 10:02

@John –

jTabGroup.setTitleAt(0,'Panel 1');

#5 Comment By Mark On March 15, 2011 @ 07:23

Hi,

Thank you for this great “un”documentation! It found instant use in my GUI. 🙂
I did realize that adding a jLabel together with a close button makes the tab look a bit odd as the label then takes the color of the figure background instead of the tab itself (which can also be seen on the above image of the tab with closebutton). I tried to find the source of this behaviour and found that adding either jLabel or close button would lead to it. In the above picture of a jLabel with an icon however everything looks normal….
Anyhow, i solved the problem by setting the jPanel ‘Opaque’-property to ‘off’, i.e.:

set(jPanel.getLayout, 'Hgap',0, 'Vgap',0);  %default gap=5px
set(jPanel,'Opaque','off');
 

Again thanks for the documentation, keep that good stuff coming!

Greetz
-Mark

#6 Comment By Etienne On March 24, 2011 @ 22:17

Hi
I really find your blog extremely useful for our GUI development.
In your close button example, you use the callback

set(jCloseButton, 'ActionPerformedCallback',@(h,e)delete(tab2));

I have no clue of what the @(h,e) construction does. Could you explain ?

#7 Comment By Yair Altman On March 25, 2011 @ 08:38

@Etienne – this is the standard way that Matlab callbacks are defined: h is the callback’ed object (in this case, jCloseButton), e is the eventData object that holds information about the callback event. A full explanation is available [12].

In this case, @(h,e) is simply an inline definition of a simple function that accepts h and e, and does “delete(tab2)” (no need for either h or e in this particular case).

#8 Comment By Yigit Koyuncuoglu On November 22, 2011 @ 02:25

Hello Yair
I have a question about using the uitree. As I understand uitree accepts generally figure handles. Is it possible to use an uitab handle instead of figure handle. For example, I have a GUI having four tabs, but I want to put an uitree object to only 2nd tab. Because uitree use figure handles, it appears in all tabs. Is it possible to solve this problem?

#9 Comment By Yair Altman On November 22, 2011 @ 03:37

@Yigit – I explained how to do this in my [13]:

uitrees are always created as a direct child of the containing figure, ignoring creation-time Parent values. However, the Parent property can be modified following the tree’s creation:

[mtree, container] = uitree('v0', 'Root','C:\', 'Parent',tab1); % Parent is ignored
set(container, 'Parent', tab1);  % fix the uitree Parent

Note: you would probably also want to fix the container’s Position property, not just it’s Parent.

#10 Comment By Johan On March 21, 2012 @ 04:51

Hello Yair,

I am using tabs aligned to the left.

It seems like the title of the first tab added to hTabGroup sets the width of all tabs.

Example:

This will create very wide tabs

 
%Create tabs
hTabGroup = uitabgroup(gcf,'TabLocation','Left'); 
tab1 = uitab(hTabGroup, 'title','1 -------------------------------------');
a = axes('parent', tab1); surf(peaks);
tab2 = uitab(hTabGroup, 'title','2');
b = uicontrol(tab2, 'Style','pushbutton','String','Close', 'Callback','close(gcbf)');

This will create very narrow tabs:

 
%Create tabs
hTabGroup = uitabgroup(gcf,'TabLocation','Left'); 
tab1 = uitab(hTabGroup, 'title','1');
a = axes('parent', tab1); surf(peaks);
tab2 = uitab(hTabGroup, 'title','2 --------------------------------------');
b = uicontrol(tab2, 'Style','pushbutton','String','Close', 'Callback','close(gcbf)');

Is there a property that can be set to adjust the tab width?

kind regards
Johan

#11 Comment By Johan On March 21, 2012 @ 05:04

Also, horizontal alignment of the title is set to centered, this looks very weird when titles of different length are used.

Is there any way to change this?

#12 Comment By Yair Altman On March 21, 2012 @ 11:14

@Johan – simply use TabLocation=’top’ or ‘bottom’. When you use ‘left’ or ‘right’ the tabs are all set to the same width, because otherwise they would not align one on top of the other. This is not a problem with top/left and so in those cases the tabs automatically use a dynamic width.

#13 Comment By Hiro On July 24, 2014 @ 16:33

In trying to make the title of tabs font size but I am having some trouble using html.

jTabGroup.setForeground(java.awt.Fontsize.9)

But this doesnt seem to work, any suggestions?
Thanks

#14 Comment By Yair Altman On July 25, 2014 @ 11:08

@Hiro – setForeground is used to set the foreground color, not the font. Try one of these alternatives:

% Alternative #1 - HTML (pure Matlab)
set(hTab, 'Title', 'tab title')

% Alternative #2 - Java (requires *NON*-HTML tab title)
jTabGroup = getappdata(handle(hTabGroup), 'JTabbedPane')
jTabGroup.setFont(java.awt.Font('Arial',0,18))

Note that HTML formatting of the title string overrides any Java-based customization.

#15 Comment By Mike On July 25, 2014 @ 12:36

Is it possible to implement this on a MATLAB uitabgroup?

% 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));

Sorry if this is obvious, I’m new to MATLAB.

#16 Comment By Yair Altman On July 25, 2014 @ 12:40

@Mike – I posted this code on this very page, so why would you think it was not possible???
If you’re not sure how it works, simply run it in your Matlab. It won’t bite – I promise.

#17 Comment By Mike On July 25, 2014 @ 21:14

I guess I wasn’t clear. What I was asking is how do you implement tho son a uitab grouped with uitabgroup to close SelectedTab. it looks like you enter set(jCloseButton, ‘ActionPerformedCallback’,@(h,e)delete(tab2)); and it will only delete the item ‘tab2’. Am I way off base here?

Thanks for the quick reply.

#18 Comment By Yair Altman On July 26, 2014 @ 19:01

@Mike – indeed: in this specific example the close-button will only close tab2. You need to set a similar ActionPerformedCallback for any close button that you create.

#19 Comment By David M On December 22, 2014 @ 16:04

Hello Mr. Altman,

I believe there is trouble with R2014b. Here is my code, just as you have it listed above:

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');

% 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));

% 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));

% 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);

% Attach this tab panel as the tab-group's 2nd component
jTabGroup.setTabComponentAt(1,jPanel)	% Tab #1 = second tab

However, when I attempt to run this script, the following error message occurs:

Attempt to reference field of non-structure array.

Error in Untitled2 (line 32)
jTabGroup.setTabComponentAt(1,jPanel) % Tab #1 = second tab

Everything worked fine in 2014a. Any ideas?

Thanks you for you time and efforts.

#20 Comment By Yair Altman On December 22, 2014 @ 16:17

@David – indeed, in R2014b, MathWorks have removed the Java peer reference from hTabGroup‘s ApplicationData, so you get an empty array. Instead, use the [14]:

jTabGroup = getappdata(handle(hTabGroup), 'JTabbedPane');
if isempty(jTabGroup)
    jTabGroup = findjobj(hTabGroup);
    jTabGroup = jTabGroup(end);  % in case several handles are returned
end

#21 Comment By David M On December 23, 2014 @ 05:02

Many thanks, Yair. Your solution works very well.

I have just ordered your new book, Accelerating Matlab Performance. I am looking forward to exploring it!

#22 Comment By Christian T On May 22, 2015 @ 10:53

Hello Yair,

when i’m trying to use your example for uitab close buttons, i always get the Error Message:
No appropriate method, property, or field setTabComponentAt for class matlab.graphics.chart.primitive.Surface. or
on this line:

 
jTabGroup.setTabComponentAt(1,jPanel)	% Tab #1 = second tab

basicly all jTabGroup Methods trigger this error message. (setTitleAt, setFont)

Matlab Version is r2014b
Java Version: Java 1.7.0_11-b21 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode

I used the findobj Workaround to get the jTabGroup handle.

#23 Comment By Yair Altman On May 24, 2015 @ 10:27

@Christian – apparently you are not accessing the tab-group handle but a plain graphics surface handle. Follow the instructions in my blog to get the correct Java tabgroup handle.

#24 Comment By gep On May 31, 2015 @ 12:31

hello, any hints why the (close) button does not show up on R2015a (linux64)?
i have a

jTabGroup =
	javahandle_withcallbacks.com.mathworks.mwswing.MJTabbedPane

from findjobj utility, so i assume i got the correct handle. Also no errors at all.. I can change icon, dimension, label and gaps but the button is not visible/functional.

#25 Comment By Carlos On September 5, 2015 @ 17:19

Good night,

I was wondering If there’s a way to edit the background color of a uitabgroup. Thanks in advance. 🙂

#26 Comment By David M On November 20, 2015 @ 15:24

Hi Yair,

In R2015b, on many occasions, I receive an empty handle (handle: 1-by-0) when attempting to get the underlying Java reference by any method (getappdata, findjobj, etc.). Take, for instance, the following script:

 
% Get the underlying Java reference (use hidden property)
jTabGroupViewing = getappdata(handle(hTabGroup), 'JTabbedPane');

if isempty(jTabGroupViewing)
    jTabGroupViewing = findjobj(hTabGroup);
    jTabGroupViewing = jTabGroupViewing(end);  % in case several handles are returned
end

Note, my figure is visible and not hidden when I attempt to run this script. Strangely, it will sometimes (randomly) return the Java reference, but it will cause the figure to sporadically scroll through the menu bar options. Quite odd behavior.

Note, also, everything worked well in R2015a.. I am running Windows 7.

#27 Comment By Yair Altman On November 21, 2015 @ 09:12

Try adding drawnow(); pause(0.1) [15]

#28 Comment By David M On November 23, 2015 @ 09:10

Hello Yair,

Thank you for the timely reply. Per you suggestion, I flushed the EDT queue and pending graphics operations using drawnow(); pause(0.1) prior to getting the underlying Java reference. However, this method too was unsuccessful in R2015b. The Java reference still comes back empty (handle: 1-by-0).

I ran the same script on R2015a, and it was successful. Have you experienced similar problems with R2015b?

#29 Comment By Ondrej On February 26, 2016 @ 17:16

Hi Yair,

I am using Matlab2008a (7.6.0) on Linux and for some reason most of your great “hacks” has no effect. Even the simple example with red italic tab title

jTabGroup.setForegroundAt(1,java.awt.Color(1.0,0,0)); % tab #1
jTabGroup.setTitleAt(1,'Panel 2');
jTabGroup.setForeground(java.awt.Color.red);

Makes the text red but not italic.

Also I cannot get to work to get any icon onto the tab (any of your examples). Is this because of the old Matlab version?

Thanks.
Ondrej

#30 Comment By Yair Altman On February 27, 2016 @ 18:30

@Ondrej – you should double-check your code to ensure that you use valid HTML syntax.

Still, many things [especially the advanced stuff that I regularly write about in this blog] will not work on such an old version as R2008a, which is almost 20 releases behind the latest Matlab release.

#31 Comment By manel On March 10, 2016 @ 14:40

please how can i change the icon in the figure (what i mean is the small icon of matlab in the top,left ?????

#32 Comment By Yishai E On October 24, 2016 @ 16:37

Hi Yair,

I’m trying to access some hidden properties (Matlab 2015b, Win7), the following code gives me an empty jTabGroup:

% 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');

I tried using findjobj as you replied to one of the questions here; if I run the no-output GUI version I can see the entire Java tree with all the objects on it, however,

 o = findjobj(hTabGroup)

returns an empty matrix. Am I missing something?

Thanks,
Yishai

#33 Comment By Dani On October 26, 2017 @ 17:17

On Matlab 2017a findjobj(hTabGroup) does not seem to find the underlying java handle any longer. Probably the same in earlier releases (see comments above). Any way we can still do uitab java magic in new Matlab versions?

#34 Comment By Yair Altman On October 26, 2017 @ 19:40

@Dani – it works fine for me – perhaps your specific GUI has a problem that causes a problem for findjobj to find the underlying java handle:

jHandles = findjobj(hTabGroup);
jTabbedPane = jHandles(end);

#35 Comment By Eric On April 4, 2018 @ 20:11

Yair,
I have a tab group positioned on the left using TabLocation and i am ok with the auto-sizing of the tabs to fit the longest tab name, but for all the tab names i would like them left justified. Trying to do this by setting the title property of the tab using an HTML string doesn’t seem to work. Is there a work around for this? Example code below:

hTabGroup2 = uitabgroup;
tab1 = uitab(hTabGroup2,'title','Tab #1');
tab2 = uitab(hTabGroup2,'title','This is a very long tab');
tab3 = uitab(hTabGroup2,'title','Tab #3');
set(hTabGroup2,'TabLocation','left')

set(tab1,'Title','Tab #1')

#36 Comment By Yair Altman On April 5, 2018 @ 18:05

@Eric – HTML alignments only work within the area that the control allocates for the contents. It so happens that Java Swing allocates a tightly-fitting area for controls, so we basically have 3 options:

  • Use a fixed-width font and right-pad with spaces
  • Get the underlying Java handle of the tab-group and modify the Java properties (this became very difficult in recent Matlab releases)
  • Enlarge the allocated area dynamically via pure Matlab and HTML: We first get the width of the tabs, then we set the tab’s title to be a left-aligned table having the same fixed width as the tab (minus a bit due to the margins):
    pos = getpixelposition(tab1);
    tab1.Title = sprintf('Tab #1', pos(1)-4);
    

#37 Comment By Thomas On April 20, 2018 @ 15:09

Hi Yair,

when I run your code:

figure 
% 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');
 
% Add an icon to tab #1 (=second tab)
icon = javax.swing.ImageIcon('Find.gif');
jLabel = javax.swing.JLabel('Tab #2');
jLabel.setIcon(icon);
jTabGroup.setTabComponentAt(1,jLabel);	% Tab #1 = second tab
 
% Note: both label & icon automatically grayed when disabled
jTabGroup.setEnabledAt(1,false);  % disable only tab #1

I get the following error message. Can you tell me what’s wrong? or Would you please send me your code that works fine?

Thanks,
Maz

#38 Comment By Michi On August 29, 2019 @ 20:13

Hi Yair,
many thanks for the excellent and very interesting work arounds! I am currently struggling with the example you provided. When I run your code

% 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');

% Equivalent manners to set a red tab foreground:
jTabGroup.setForegroundAt(1,java.awt.Color(1.0,0,0)); % tab #1
jTabGroup.setTitleAt(1,'Panel 2');
jTabGroup.setForeground(java.awt.Color.red);

% Equivalent manners to set a yellow tab background:
jTabGroup.setTitleAt(0,'Panel 1');
jTabGroup.setTitleAt(0,'Panel 1');

the following error shows up:
Dot indexing is not supported for variables of this type.

Executing your code line by line indicates that the error is prompted for the first time when executing
jTabGroup.setForegroundAt(1,java.awt.Color(1.0,0,0)); % tab #1

I am using MATLAB Version: 9.5.0.1033004 (R2018b) Update 2.

Many thanks for your help!

#39 Comment By Yair Altman On September 10, 2019 @ 13:32

@Michi – in recent Matlab releases Matlab no longer stores the Java handle in the tab-group’s ApplicationData property, so using getappdata() returns an empty array, causing the error that you see. Instead of using getappdata, us the [14]:

jTabGroup = findjobj(hTabGroup,'class','MJTabbedPane');

#40 Comment By Peter On November 8, 2021 @ 21:53

Hello again Mr. Altman and thank you for all you work and help!
I have a question. I want to resize the TABS, i mean the small things at the top, that says “tab1, tab2,….” they are incredible small. I searched an eternity for a solution but cant find it. I tried fontsize and jLabel size but they are not resizing. They stay in a fixed height. the width is stretching accordingly to the amount of text but not the height itself. Do you know a solution? I am working with Matlab 2013b.
Greetings
Peter

#41 Comment By Yair Altman On November 8, 2021 @ 22:23

@Peter – tab heights are set by the Operating System’s Look and Feel. You can either [16], or use the much larger tabs in the [17].

#42 Comment By Peter On November 9, 2021 @ 11:50

It’s working! THANK YOU!!

#43 Comment By Niko On February 15, 2024 @ 16:34

jTabGroup = getappdata(handle(hTabGroup),'JTabbedPane');
doesn’t work anymore in R2023b 🙁

when i do
jTabGroup = findjobj(hTabGroup,'class','MJTabbedPane');

I get an object of class
javahandle_withcallbacks.com.mathworks.mwswing.MJTabbedPane

But when I call e.g.
jTabGroup.setForegroundAt(0,java.awt.Color(1.0,1,1));

on it, I just get
“Incorrect number or types of inputs or outputs for function setForegroundAt.”


Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/uitab-colors-icons-images

URLs in this post:

[1] Matlab’s semi-documented tab-panel functionality: http://undocumentedmatlab.com/blog/tab-panels-uitab-and-relatives/

[2] asked a related question: http://undocumentedmatlab.com/blog/customizing-uicontrol-border/#comment-21714

[3] CSS background: http://www.w3schools.com/css/css_background.asp

[4] CSS color directive: http://www.w3schools.com/css/css_colors.asp

[5] comment: http://undocumentedmatlab.com/blog/uitab-colors-icons-images/#respond

[6] Uitab customizations : https://undocumentedmatlab.com/articles/uitab-customizations

[7] Tab panels – uitab and relatives : https://undocumentedmatlab.com/articles/tab-panels-uitab-and-relatives

[8] Setting system tray icons : https://undocumentedmatlab.com/articles/setting-system-tray-icons

[9] Images in Matlab uicontrols & labels : https://undocumentedmatlab.com/articles/images-in-matlab-uicontrols-and-labels

[10] Matlab toolstrip – part 5 (icons) : https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons

[11] Icon images & text in Matlab uicontrols : https://undocumentedmatlab.com/articles/icon-images-in-matlab-uicontrols

[12] : http://www.mathworks.com/help/techdoc/creating_plots/f7-55506.html

[13] : https://undocumentedmatlab.com/blog/uitree/

[14] : https://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object

[15] : https://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt

[16] : https://undocumentedmatlab.com/articles/modifying-matlab-look-and-feel

[17] : https://www.mathworks.com/matlabcentral/fileexchange/27758-gui-layout-toolbox

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.