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

Setting status-bar components

July 30, 2009 20 Comments

I last week’s post, Setting status-bar text, I showed how to set the status-bar text in Matlab figures and the main desktop. But Matlab status-bars are Java containers in which we can add GUI controls, not just simple text labels. In this post I will show how to do this for Matlab 7 figures.
Let’s return to the two alternatives I’ve presented in my previous post for setting a Matlab 7 figure status-bar text:

% Alternative #1 (hFig = requested figure's handle)
jFrame = get(hFig,'JavaFrame');
jFigPanel = get(jFrame,'FigurePanelContainer');
jRootPane = jFigPanel.getComponent(0).getRootPane;
jRootPane = jRootPane.getTopLevelAncestor;
statusbarObj = jRootPane.getStatusBar;
statusbarObj.setText(statusText);
jRootPane.setStatusBarVisible(1);
% Alternative #2
jFrame = get(hFig,'JavaFrame');
jRootPane = jFrame.fFigureClient.getWindow;
statusbarObj = com.mathworks.mwswing.MJStatusBar;
jRootPane.setStatusBar(statusbarObj);
statusbarObj.setText(statusText);

% Alternative #1 (hFig = requested figure's handle) jFrame = get(hFig,'JavaFrame'); jFigPanel = get(jFrame,'FigurePanelContainer'); jRootPane = jFigPanel.getComponent(0).getRootPane; jRootPane = jRootPane.getTopLevelAncestor; statusbarObj = jRootPane.getStatusBar; statusbarObj.setText(statusText); jRootPane.setStatusBarVisible(1); % Alternative #2 jFrame = get(hFig,'JavaFrame'); jRootPane = jFrame.fFigureClient.getWindow; statusbarObj = com.mathworks.mwswing.MJStatusBar; jRootPane.setStatusBar(statusbarObj); statusbarObj.setText(statusText);

The first alternative uses the default status-bar (a com.mathworks.mwswing.MJStatusBar object) which Matlab automatically creates with each figure. This object is created as invisible, so we need to set its text (via its setText method), and then make it visible (via its ancestor’s root-pane’s setStatusBarVisible method – setting the object itself to visible is not enough). The alternative is to replace the default status-bar object with a user-specified container (in this case, we use a new instance of com.mathworks.mwswing.MJStatusBar).
Last week I forgot to mention that only the second alternative works in the general case – it appears that the first alternative often fails because figures are not created with the default statusbar on many platforms/Matlab release (I’m not sure exactly which). My StatusBar utility on the MathWorks File Exchange, which I mentioned in last week’s post, takes care of these nuances, and automatically creates the status bar object if it is missing.
In any case, using these two code snippets as a baseline, we can customize any Java container that we wish. We are not limited to text labels. The default ststusbar container, MJStatusBar, only includes a single JLabel-derived component that stores the text message. We can add other components to this container. For example, let’s add a simple progress-bar:

jFrame = get(hFig,'JavaFrame');
jRootPane = jFrame.fFigureClient.getWindow;
statusbarObj = com.mathworks.mwswing.MJStatusBar;
% Add a progress-bar to left side of standard MJStatusBar container
jProgressBar = javax.swing.JProgressBar;
set(jProgressBar, 'Minimum',0, 'Maximum',500, 'Value',234);
statusbarObj.add(jProgressBar,'West');  % 'West' => left of text; 'East' => right
% Beware: 'East' also works but doesn't resize automatically
% Set this container as the figure's status-bar
jRootPane.setStatusBar(statusbarObj);
% Note: setting setStatusBarVisible(1) is not enough to display the status-bar
% - we also need to call setText(), even if only with an empty string ''
statusbarObj.setText('testing 123...');
jRootPane.setStatusBarVisible(1);

jFrame = get(hFig,'JavaFrame'); jRootPane = jFrame.fFigureClient.getWindow; statusbarObj = com.mathworks.mwswing.MJStatusBar; % Add a progress-bar to left side of standard MJStatusBar container jProgressBar = javax.swing.JProgressBar; set(jProgressBar, 'Minimum',0, 'Maximum',500, 'Value',234); statusbarObj.add(jProgressBar,'West'); % 'West' => left of text; 'East' => right % Beware: 'East' also works but doesn't resize automatically % Set this container as the figure's status-bar jRootPane.setStatusBar(statusbarObj); % Note: setting setStatusBarVisible(1) is not enough to display the status-bar % - we also need to call setText(), even if only with an empty string '' statusbarObj.setText('testing 123...'); jRootPane.setStatusBarVisible(1);


Status bar with a simple progress-bar
Status bar with a simple progress-bar


We can of course use the progress-bar and status-bar handles to modify their appearance within our code, for example within some loop. For example:

numIds = length(allIds);
set(jProgressBar, 'StringPainted','on', 'Maximum',numIds, 'Value',0);
for id = 1 : numIds
   % Update status bar
   set(jProgressBar, 'StringPainted','on', 'Value',id);
   msg = 'Processing %d of %d (%.1f%%)...';
   statusbarObj.setText(sprintf(msg,id,numIds,100*id/numIds));
   % do something useful...
end  % for all Ids
% Some final status-bar updates...
% Hide the progress-bar
jProgressBar.setVisible(0);
% Hide the corner grip
cornerGrip = statusbarObj.getParent.getComponent(0);
cornerGrip.setVisible(0);  % or: set(cornerGrip,'Visible','off')
% Set a red foreground & yellow background to status bar text
statusbarObj.setText('All done - congratulations!!!');
statusbarTxt = statusbarObj.getComponent(0);
statusbarTxt.setForeground(java.awt.Color.red);
set(statusbarTxt,'Background','yellow');
set(statusbarTxt,'Background',[1,1,0]);  % an alternative…

numIds = length(allIds); set(jProgressBar, 'StringPainted','on', 'Maximum',numIds, 'Value',0); for id = 1 : numIds % Update status bar set(jProgressBar, 'StringPainted','on', 'Value',id); msg = 'Processing %d of %d (%.1f%%)...'; statusbarObj.setText(sprintf(msg,id,numIds,100*id/numIds)); % do something useful... end % for all Ids % Some final status-bar updates... % Hide the progress-bar jProgressBar.setVisible(0); % Hide the corner grip cornerGrip = statusbarObj.getParent.getComponent(0); cornerGrip.setVisible(0); % or: set(cornerGrip,'Visible','off') % Set a red foreground & yellow background to status bar text statusbarObj.setText('All done - congratulations!!!'); statusbarTxt = statusbarObj.getComponent(0); statusbarTxt.setForeground(java.awt.Color.red); set(statusbarTxt,'Background','yellow'); set(statusbarTxt,'Background',[1,1,0]); % an alternative…


Modifying status bar properties in run-timer
Modifying status bar properties in run-time


The progress-bar component was just a simple example of how to present non-intrusive controls/information in the figure status bar. Other controls (buttons, checkboxes etc.) can similarly be added.
Note that the status-bar’s corner-grip (at its far right) is not a sub-component of the statusbarObj object like the label, but rather of its parent JPanel container. This is easily be seen using my FindJObj utility on the MathWorks File Exchange:

Status bar with a simple progress-bar
Status bar with a simple progress-bar


One final note: The status bar is 20 pixels high across the entire bottom of the figure. It hides everything between pixel heights 0-20, even parts of uicontrols, regardless of who was created first or the relative ComponentZOrder in the frame’s ContentPane:

% Add a "Next phase" button to the right of the text
jb = javax.swing.JButton('Next phase >');
jbh = handle(jb,'CallbackProperties');
set(jbh, 'ActionPerformedCallback', @nextPhaseFunction);
statusbarObj.add(jb,'East');
%note: we might need jRootPane.setStatusBarVisible(0)
% followed by jRootPane.setStatusBarVisible(1) to repaint
% Add a simple Matlab uicontrol, obscured by the status-bar
hb = uicontrol('string','click me!', 'position',[10,15,70,30]);

% Add a "Next phase" button to the right of the text jb = javax.swing.JButton('Next phase >'); jbh = handle(jb,'CallbackProperties'); set(jbh, 'ActionPerformedCallback', @nextPhaseFunction); statusbarObj.add(jb,'East'); %note: we might need jRootPane.setStatusBarVisible(0) % followed by jRootPane.setStatusBarVisible(1) to repaint % Add a simple Matlab uicontrol, obscured by the status-bar hb = uicontrol('string','click me!', 'position',[10,15,70,30]);


Adding controls inside and outside the status-bar
Adding controls inside and outside the status-bar


If you have made interesting use of Matlab’s status-bar, please share them in the comments section below.

Related posts:

  1. Setting status-bar text – The Matlab desktop and figure windows have a usable statusbar which can only be set using undocumented methods. This post shows how to set the status-bar text....
  2. Figure toolbar components – Matlab's toolbars can be customized using a combination of undocumented Matlab and Java hacks. This article describes how to access existing toolbar icons and how to add non-button toolbar components....
  3. Date selection components – The JIDE package, pre-bundled in Matlab, contains several GUI controls for selecting dates - this article explains how they can be used...
  4. Color selection components – Matlab has several internal color-selection components that can easily be integrated in Matlab GUI...
  5. Font selection components – Several built-in components enable programmatic font selection in Matlab GUI - this article explains how. ...
  6. Checking status of warning messages in MEX – Undocumented Mex functions can be used to extract the state of Matlab warnings in run-time. ...
FindJObj GUI Java JavaFrame
Print Print
« Previous
Next »
20 Responses
  1. Aurélien Queffurust October 22, 2009 at 02:52 Reply

    Hello Yair,

    I am using statusbar in my codes since I prefer waitbar docked. I wanted to know how to delete this object in a safe way.
    In the help, I can read that the command

    statusbar;

    statusbar;

    deletes status bar from current figure. But I had to comment the line 263 :

    lasterr

    lasterr

    to prevent the message : Property Name already in use . It happens each time I recreate a statusbar or when I display a dialog box.

    I guess that :

    jProgressBar.setVisible(0);

    jProgressBar.setVisible(0);

    is not enough to delete this object.

    Thanks,
    Aurélien

    • Yair Altman October 22, 2009 at 15:49 Reply

      Aurélien – you are correct: For performance reasons, the statusbar object is not really deleted when you issue

      statusbar;

      statusbar;

      Instead, it is simply made invisible using the root-pane’s setStatusBarVisible(0) method in line #222. Then, when you recreate the statusbar, adding the internal handles to the CornerGrip, TextPanel and ProgressBar fails, since these properties already exist in the old (previously-hidden) statusbar object.

      Your solution, to comment line #263, is probably the easiest and best.

  2. nir S. January 5, 2013 at 23:55 Reply

    Hi Yair,

    It seems that matlab won’t support JavaFrame in future versions of Matlab.
    are there any other way to intgrated the waitbar in my GUI?

    • Yair Altman January 7, 2013 at 03:09 Reply

      @Nir – no. All the fancy GUI stuff needs Java. You can use this on all Matlab releases of the past decade. If you are scared about future releases then settle for Matlab’s plain-vanilla GUI or use a non-Matlab GUI to begin with.

      • nir S. January 10, 2013 at 00:43

        thanks

  3. luis camilo August 9, 2014 at 22:25 Reply

    How can I modify the position of the statusbar in a GUIDE, for example [10,10, 150, 200]? thanks!

    • Yair Altman August 9, 2014 at 23:23 Reply

      @Luis – you cannot do it in GUIDE, only programmatically

  4. Luis Camilo January 11, 2015 at 22:11 Reply

    Thanks for yours answers Yair.
    Could you help me with these questions?
    1. How I can include spinning icon in the status bar?
    2. How I can close the place where the status bar and the Paneltext is loacted , after having used it and and change its color to transparent?
    3. How I can change the dimension of the status bar?
    Thank You for your time and patience with my questions..

  5. Mark January 23, 2015 at 02:40 Reply

    Dear Yair,

    Thanks for again a wonderful contribution!

    However, I found a small bug, at least on my system. When creating a statusbar using your ‘statusbar’ function from the File Exchange, the other controls are properly placed in the beginning. But when I then move the window, all controls suddenly shift down (I guess 20 pixels). Is seems that MATLAB is not aware of the ‘extra space’ for the status bar when redrawing the controls.

    I use MATLAB 2014b on Windows 7. I cannot test if this is also the case in older versions.

    Is this a problem on my system, or is it solvable in the statusbar function?

    Thanks in advance!

    Best,
    Mark

    • Greg February 10, 2015 at 07:26 Reply

      I have been having this problem since the R2014b prerelease on both Windows and Linux platforms.

      I seem to have gotten a reasonably stable workaround by adding the following lines to the end of my function that creates the status bar:

      old_pos = get(h_fig,'Position');
      set(h_fig,'Position',1.05.*old_pos);
      set(h_fig,'Position',old_pos);

      old_pos = get(h_fig,'Position'); set(h_fig,'Position',1.05.*old_pos); set(h_fig,'Position',old_pos);

      This seems to be giving me a consistent appearance when the GUI is first displayed. If it is then resized graphically (by dragging the window corner), the GUI components still jump up and down randomly. But, if you disable window resizing (or, you know, just don’t try to resize it), the GUI appears stable.

      Interestingly, if you don’t store the old position before the first change, you can’t appear to recreate it from the new position. For example, the following code does not work correctly:

      set(h_fig,'Position',1.05.*get(h_fig,'Position'));
      set(h_fig,'Position',get(h_fig,'Position')./1.05);

      set(h_fig,'Position',1.05.*get(h_fig,'Position')); set(h_fig,'Position',get(h_fig,'Position')./1.05);

  6. Shalin January 15, 2016 at 17:52 Reply

    Hello again,

    So I was trying around status bar and I was trying to add a gridlayout or flowlayout to it so that I can do whatever I like with the layout of status bar but somehow none of the layouts allow statusbar handle. Any idea on that? I know, I can add any JAVA component once I have a layout handle so I was trying to add a gridlayout to status bar and then a couple of JAVA UI components to add.

    • Shalin January 15, 2016 at 18:05 Reply

      Also, I tried your way of making JAVA root frame but it gives error.

      RootPane = jFrame.fFigureClient.getWindow;
      No appropriate method, property, or field ‘fFigureClient’ for class ‘com.mathworks.hg.peer.HG2FigurePeer’.

      I think, it doesn’t work on Matlab 2015 as they suggest here – http://www.mathworks.com/matlabcentral/answers/196352-sim-i-am-gets-an-error

      Not sure though! Sorry for spamming.

      • Yair Altman January 16, 2016 at 19:18
        statusbarObj = com.mathworks.mwswing.MJStatusBar;
        newLayout = java.awt.GridLayout(2,3);  % 2 rows, 3 cols
        statusbarObj.setLayout(newLayout);
        ...  % add content to the statusbarObj container
        rootPane = jFrame.fHG2Client.getWindow;  % See http://undocumentedmatlab.com/blog/hg2-update#observations item #2
        rootPane.setStatusBar(statusbarObj);

        statusbarObj = com.mathworks.mwswing.MJStatusBar; newLayout = java.awt.GridLayout(2,3); % 2 rows, 3 cols statusbarObj.setLayout(newLayout); ... % add content to the statusbarObj container rootPane = jFrame.fHG2Client.getWindow; % See http://undocumentedmatlab.com/blog/hg2-update#observations item #2 rootPane.setStatusBar(statusbarObj);

  7. anon April 21, 2016 at 06:41 Reply

    i want to make status bar that when user enter their name, the status state ‘User enter Name’ when user want to enter the age, the status change to ‘User enter Age’. How can i make it like that ?

    • Yair Altman April 21, 2016 at 13:37 Reply

      @anon – see here: http://undocumentedmatlab.com/blog/setting-status-bar-text

  8. Jan Grajciar December 12, 2018 at 21:36 Reply

    Hi,
    did anyone find a way to fix the controls, so they do not move when I change the window size?

    Thx
    Jan

    • Yair Altman February 15, 2019 at 12:48 Reply

      Jan – if you mean the controls in the statusbar, the only controls that change their relative position are the ones that are attached to right (“East”) border, such as the “Next phase” button in my example in the post. If you wish them to remain in the same relative position, attach them to the left (“West”) instead.

  9. Gu Bo Yu January 16, 2019 at 06:21 Reply

    hi,I have a problem when useing the statusbar.
    I find that the statusbar will be vanished when exsiting two docked figures.
    May you give me some advice?
    Thanks

    • Gu Bo Yu January 16, 2019 at 06:31 Reply

      Hi
      The statusbar will vanished when there exsiting two or more docked figures.
      The statusbar needed to be fixed in the container?
      May you give me some advices?
      Thanks!

    • Yair Altman February 15, 2019 at 12:51 Reply

      Matlab’s statusbar hack only works with non-docked figures. If you need them to work also in docked figures, replicate the functionality using regular panels.

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