Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

Transparent uipanels

April 9, 2014 17 Comments

A well-known Matlab limitation is that component position vectors must be specified as either ‘normalized’ or some fixed-sized value, but not in combination. For example, we cannot place a button in the center of the screen (normalized x/y=0.5) with a fixed sized width/height. Either all position elements are normalized, or all are fixed. Most Matlab GUIs that I’ve seen keep the normalized units, with the result that buttons look bloated or shrunken when the figure is resized:

hButton = uicontrol('Units','norm', 'Position',[0.5,0.5,0.15,0.15], 'String','click me!');

hButton = uicontrol('Units','norm', 'Position',[0.5,0.5,0.15,0.15], 'String','click me!');

bad-looking resized normalized button
bad-looking resized normalized button

A common way to address this latter limitation is to use a borderless containing uipanel at the requested normalized position, and place a fixed-sized component as a child of that panel:

bgColor = get(gcf,'Color');
hPanel = uipanel('Units','norm', 'Position',[0.5, 0.5, 0.45, 0.45], 'BorderType','none', 'BackgroundColor',bgColor);
hButton = uicontrol('Parent',hPanel, 'Units','pixels', 'Position',[0,0,60,20], 'String','click me!');

bgColor = get(gcf,'Color'); hPanel = uipanel('Units','norm', 'Position',[0.5, 0.5, 0.45, 0.45], 'BorderType','none', 'BackgroundColor',bgColor); hButton = uicontrol('Parent',hPanel, 'Units','pixels', 'Position',[0,0,60,20], 'String','click me!');

fixed-size button at normalized position
fixed-size button at normalized position

This works well in simple figures, where there is nothing beneath the panel/control. Resizing the figure will keep the button centered, while preserving its 60×20 size. But what happens if we have an axes beneath? In this case we encounter another Matlab limitation, that a top-most control always occludes (hides) an overlapped component. This is due to the fact that Matlab GUI uses heavyweight controls and containers, which do not allow transparency. For this reason, we cannot show a plot chart as the background of a table or listbox, for example. The result is that our panel will occlude the axes for a horrible-looking effect:

t = 1:.01:100;
plot(t, cos(t), 'r');
bgColor = get(gcf,'Color');
hPanel = uipanel('Units','norm', 'Position',[0.5, 0.5, 0.45, 0.45], 'BorderType','none', 'BackgroundColor',bgColor);
hButton = uicontrol('Parent',hPanel, 'Units','pixels', 'Position',[0,0,60,20], 'String','click me!');

t = 1:.01:100; plot(t, cos(t), 'r'); bgColor = get(gcf,'Color'); hPanel = uipanel('Units','norm', 'Position',[0.5, 0.5, 0.45, 0.45], 'BorderType','none', 'BackgroundColor',bgColor); hButton = uicontrol('Parent',hPanel, 'Units','pixels', 'Position',[0,0,60,20], 'String','click me!');

panel occluding a plot chart
panel occluding a plot chart

Which is where today’s undocumented feature comes in handy: simply set the uipanel’s BackgroundColor to ‘none’ and the panel will become transparent:

set(hPanel,'BackgroundColor','none')

set(hPanel,'BackgroundColor','none')

transparent panel
transparent panel

The reason that uipanel does not obey the occlusion limitation is simply because it is not really a control at all: Most Matlab GUI controls are simple wrappers for the standard Java Swing component (buttons for JButton, listboxes for JListbox etc.). However, uipanel is not a wrapper for JPanel, as we might have expected, but rather implemented as as internal Matlab component that groups HG objects for onscreen display, and possibly paints the border (a uipanel‘s title is a simple text control once again) as a simple rectangle. This is the reason why Matlab’s borders look so different than JPanel‘s. This enabled Matlab to include the back-door of the ‘none’ BackgroundColor, which works for panels but not for any other GUI control.
While BackgroundColor is a well-documented property, the ability to specify a colorspec of ‘none’ is not documented.
Note that setting the BackgroundColor to ‘none’ issues the following warning that you can ignore at your own risk:

Warning: "Setting the ColorSpec to 'none' for a uicontainer will not be allowed in a future release."

Warning: "Setting the ColorSpec to 'none' for a uicontainer will not be allowed in a future release."

To ignore (and stop issuing) such warnings, simply issue the following Matlab command:

warning  off    MATLAB:hg:ColorSpec_None
warning('off', 'MATLAB:hg:ColorSpec_None');  % equivalent alternative

warning off MATLAB:hg:ColorSpec_None warning('off', 'MATLAB:hg:ColorSpec_None'); % equivalent alternative

p.s. – using a transparent panel in this manner is not necessary in certain cases (e.g., when the axes is added after the control, not before it as above). However, relying on such side-effects is not advised at all, for obvious reasons.
I look forward to the day when Matlab controls will be able to become truly transparent, and when the position and size units could be specified separately, so that use of such back-door kludges will not be necessary. After all, designing a GUI is supposed to resemble an art, not brain surgery…

Addendum for HG2 (R2014b onward)

In HG2, the panel’s BackgroundColor can no longer be set to 'none'. Luckily, in HG2 we have a direct reference to the underlying Java JPanel component, as I explained here. We can use this reference by setting it, as well as its direct parent and child components, to be non-opaque, as follows:

jPanel = hPanel.JavaFrame.getPrintableComponent;  % hPanel is the Matlab handle to the uipanel
jPanel.setOpaque(false)
jPanel.getParent.setOpaque(false)
jPanel.getComponent(0).setOpaque(false)
jPanel.repaint

jPanel = hPanel.JavaFrame.getPrintableComponent; % hPanel is the Matlab handle to the uipanel jPanel.setOpaque(false) jPanel.getParent.setOpaque(false) jPanel.getComponent(0).setOpaque(false) jPanel.repaint

Related posts:

  1. Panel-level uicontrols – Matlab's uipanel contains a hidden handle to the title label, which can be modified into a checkbox or radio-button control...
  2. Transparent legend – Matlab chart legends are opaque be default but can be made semi- or fully transparent. ...
  3. Customizing Matlab uipanels – Matlab uipanel controls can be customized using Java in ways that are impossible with plain Matlab. ...
  4. Persisting transparent colors in HG2 – We can set semi- and fully-transparent colors in HG2 for multiple graphic objects, but making these settings stick is non-trivial. ...
  5. Transparent labels – Matlab labels can be set to a transparent background as well as padding. ...
  6. Transparent Matlab figure window – Matlab figure windows can be made fully or partially transparent/translucent or blurred - this article explains how...
GUI Handle graphics Pure Matlab Undocumented feature
Print Print
« Previous
Next »
17 Responses
  1. Dani April 11, 2014 at 00:24 Reply

    I also look forward to the day when Matlab will provide 2D graphics that one can proudly show to somebody else, 3D graphics that that at least go in the same direction as what we have with the free ParaView, and indeed a GUI toolset that is both fast and functional. Every time a new release comes out I check if they made any progress. Nothing ever happens. Maybe a new ribbon design here and there. It looks like some time in the past they developed something that was marginally better than gnuplot and ever since they have been happy with that achievement. In the meantime we are well into the third millennium and every web browser can, with the help of javascript libraries, make plots that look like they were made today and not in the dark ages.
    I am sorry about this nonconstructive rant, but I am frustrated that we have to make such efforts to circumvent the inadequate limitations of Matlab. At least with Yair’s help it is a bit easier.

  2. Ralf Hielscher April 19, 2014 at 13:16 Reply

    Hi Yair,

    maybe you could explain at this point how Mathworks achieves the positioning of the colorbar, which is of fixed width and relative position as well.

    Thanks,

    Ralf.

  3. Patrick Hogan July 29, 2014 at 17:00 Reply

    Another solution for the example given above is to set the uipanel location outside the figure. For example, to anchor the uicontrol to the center-top of the figure, use:

    hPanel = uipanel('Units','norm', 'Position',[0.5, 1, 0.01, 0.01]);
    hButton = uicontrol('Parent',hPanel, 'Units','pixels', 'Position',[-30,-20,60,20], 'String','click me!');

    hPanel = uipanel('Units','norm', 'Position',[0.5, 1, 0.01, 0.01]); hButton = uicontrol('Parent',hPanel, 'Units','pixels', 'Position',[-30,-20,60,20], 'String','click me!');

    It can cause some odd (but manageable) stacking effects, but it works well for keeping a title or header line centered and top — and you don’t have to worry about it going away in a future version or about a transparent uipanel preventing user interaction with a plot/uicontrol.

    • Yair Altman July 29, 2014 at 19:48 Reply

      @Patrick – nice idea, I like it.

  4. Ortmann February 24, 2015 at 05:53 Reply

    It seems, that the ‘none’ option also works for the TextBoxHandle-BackgroundColor of a DataCursor.

  5. Chris April 29, 2015 at 12:35 Reply

    The solution

    set(hPanel,'BackgroundColor','none')

    set(hPanel,'BackgroundColor','none')

    does not work in MATLAB versions 2014b and 2015a.
    Is there an another solution for the above-mentioned releases?

    Thanks a lot,
    Chris

    • Yair Altman April 29, 2015 at 14:32 Reply

      @Chris – perhaps @Patrick’s solution above can help you in HG2 (R2014b+).

      • Chris April 29, 2015 at 17:02

        @ Yair Altman:
        Thank you for the hint. I think that I can’t use Patrick’s solution for my gui problem.
        I need a specific position of the panel (with some buttons) on the plot,
        because if the mouse cursor of the user is on the panel area then the panel is shown.

      • Chris April 30, 2015 at 09:04

        I now have a solution for my specific problem:
        I use for the axes and the buttons a parent panel and create the buttons after the creation of the axes.
        If the mouse cursor is in a specific defined area, then the buttons’ visibility property is on, else off.
        It seems to work for my problem.

        Thanks a lot, nice website!

        Best regards,
        Chris

  6. Bruce October 16, 2015 at 10:07 Reply

    It seems in 15b things got really difficult. I would like to very simply place a rectangular outline on the figure telling the user to ‘look here’, regardless of what is under the box.

    This would be easy if panels could be transparent – but they can’t anymore.

    Additionally Patrick’s solution doesn’t work any more because panels ALWAYS clip their contents so that I cannot hide the panel outside the figure and then offset a rectangle to be in the figure. I have wanted panels to clip their controls for ages and now they overdo it. Careful what you wish for…

    Any ideas how to simply overlay a rectangle on a figure regardless of what is underneath it?

  7. Martin Kabelka June 26, 2017 at 12:32 Reply

    Hi Yair

    Have you had any thoughts how to add a transparent uipanel to the GUI in recent Matlab versions (post 15a)? I’m using GUILayout toolbox and so all my uicontrols and text labels are positioned on uipanels and I’d like to add a background image to the whole GUI.

    Thanks to your other posts I can make various uicontrols and text labels transparent but the same trick doesn’t work with uipanels, so as soon as the uncontrol/label is positioned within a panel the transparency doesn’t work.

    Any ideas?

    • Yair Altman June 27, 2017 at 19:22 Reply

      @Martin – I am not aware of a solution to make uipanels transparent in 15b onward

    • Yair Altman January 10, 2018 at 22:26 Reply

      I just added an addendum to the main post that explains how to set the uipanel’s background to transparent in HG2 (R2014b onward)

  8. Tom Benson December 26, 2018 at 10:54 Reply

    Hi Yair

    Thanks for all your great workarounds.

    I tried your last suggestion and indeed the uipanel becomes invisible. However, if the panel has any children then this does not appear to work. Is this something you have observed? I have basically been stuck on R2014a for years now because I can’t find a solution.

    My application uses uipanels as GIS layers so they need to be transparent. Of course I could try recoding to just use axes, but the panels allows simpler resizing and moving of the layers and the code is large so would require a lot of work.

  9. Maurizio Giardina March 3, 2020 at 10:14 Reply

    Hi Yair,
    make transparent panel background is what I need for my application, but I have a problem:the panels I use have also a title, and the background of the title is not transparent. The effect is not so good, but I don’t know how can I do.
    Any suggestions?
    Is it possible apply a setOpaque method also to the title?
    Thank

    • Yair Altman March 3, 2020 at 11:29 Reply

      @Maurizio – instead of using the integrated Panel title, you can use a separate label control (uicontrol('Style','text',...) or a JLabel). Alternatively, you can customize the uipanel’s title.
      * http://undocumentedmatlab.com/articles/customizing-matlab-uipanels
      * http://undocumentedmatlab.com/articles/panel-level-uicontrols

  10. Matthias Brenneis September 18, 2020 at 19:10 Reply

    Hi,

    thank you so much for the inspiring work and comments!

    My issue is: As soon as the panel gets an axes, the panel is no longer transparent:

    f = figure('Units','pixels','Position',[0 0 1000 800]);
     
    p1 = uipanel(f,'Units','pixels','Position',[0 0 500 500] , 'BorderType','none');
    a = axes(p1,'Units','pixels','Position',[0 0 400 200]);
     
    p2 = uipanel(f,'Units','pixels','Position',[200 0 500 500 ] , 'BorderType','none');
    drawnow;
     
    j = p2.JavaFrame;
    jPanel = j.getPrintableComponent;
    jPanel.setOpaque(false)
    jPanel.getParent.setOpaque(false)
    jPanel.getComponent(0).setOpaque(false)
    jPanel.repaint
    pause( 2 )
     
    % till now panel2 is transparent and axes1 is fully visible...
    a2 = axes(p2,'Units','pixels','Position',[0 200 400 200]); % This line breaks panel 2 transparency somehow...

    f = figure('Units','pixels','Position',[0 0 1000 800]); p1 = uipanel(f,'Units','pixels','Position',[0 0 500 500] , 'BorderType','none'); a = axes(p1,'Units','pixels','Position',[0 0 400 200]); p2 = uipanel(f,'Units','pixels','Position',[200 0 500 500 ] , 'BorderType','none'); drawnow; j = p2.JavaFrame; jPanel = j.getPrintableComponent; jPanel.setOpaque(false) jPanel.getParent.setOpaque(false) jPanel.getComponent(0).setOpaque(false) jPanel.repaint pause( 2 ) % till now panel2 is transparent and axes1 is fully visible... a2 = axes(p2,'Units','pixels','Position',[0 200 400 200]); % This line breaks panel 2 transparency somehow...

    It would be so helpful to have invisible Panels with axes! Especially because of the fact, that axes can not overlay “foreign” panels…

    Does anybody have a clue?

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitable (6) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
Contact us
Captcha image for Custom Contact Forms plugin. You must type the numbers shown in the image
Undocumented Matlab © 2009 - Yair Altman
This website and Octahedron Ltd. are not affiliated with The MathWorks Inc.; MATLAB® is a registered trademark of The MathWorks Inc.
Scroll to top