- Undocumented Matlab - https://undocumentedmatlab.com -
Figure toolbar customizations
Posted By Yair Altman On September 2, 2009 | 24 Comments
Last week, I described [1] how to access existing Matlab figure toolbar icons and how to add non-button toolbar components. Today, I describe how the toolbar itself can be customized using undocumented functionality and properties.
All the important undocumented customizations can only be accessed via the toolbar’s Java handle, which is retrieved so:
hToolbar = findall(hFig,'tag','FigureToolBar');
jToolbar = get(get(hToolbar,'JavaContainer'),'ComponentPeer');
One interesting functionality is enabling a floating toolbar, via jToolbar.setFloatable(1). The toolbar can then be dragged from its docked position at the top of the figure menu, becoming enclosed in an independent floating window (a non-modal javax.swing.JDialog child of the parent figure, to be exact). Since this toolbar window has a very small initial size and no name, a simple immediate fix is required:
% Modify Java toolbar properties
jToolbar.setFloatable(1);
hjToolbar = handle(jToolbar,'CallbackProperties');
set(hjToolbar,'AncestorAddedCallback',@dockUndockCallbackFcn);
% Sample dockUndockCallbackFcn function
function dockUndockCallbackFcn(hjToolbar, eventdata)
if hjToolbar.isFloating
jToolbarWin = hjToolbar.getTopLevelAncestor;
jToolbarWin.setTitle('Toolbar');
%jToolbarWin.setResizable(1); %if you wish manual resize
jToolbarWin.setPreferredSize(java.awt.Dimension(380,57));
jToolbarWin.setSize(java.awt.Dimension(380,57));
jToolbar.revalidate; %repaint toolbar
jToolbarWin.getParent.validate; %repaint parent figure
end
end
jToolbar.setBackground(java.awt.Color.cyan); %or: Color(0,1,1)
% Set non-default Rollover, MorePopupEnabled
jToolbar.setRollover(0); % or: set(jToolbar,'Rollover','off');
jToolbar.setMorePopupEnabled(0); % or: set(jToolbar,'MorePopupEnabled','off');
% Prepare the red border panel
oldBorder = jComponent.getBorder;
redBorder = javax.swing.border.LineBorder(java.awt.Color.red,2,0);
redBorderPanel = javax.swing.JPanel;
redBorderPanel.setBorder(redBorder);
redBorderPanel.setOpaque(0); % transparent interior, red border
redBorderPanel.setBounds(jComponent.getBounds);
isSettable(compIdx) = ismethod(jComponent,'setBorder');
% flash by periodically displaying/hiding the panel
for idx = 1 : 2*numTimes
if idx>1, pause(delaySecs); end % don't pause at start
visible = mod(idx,2);
jParent = jComponent.getParent;
% Most Java components allow modifying their borders
if isSettable
if visible
% Set a red border
jComp.setBorder(redBorder);
try jComponent.setBorderPainted(1); catch, end
else %if ~isempty(oldorder)
% Remove red border by restoring the original border
jComp.setBorder(oldBorder);
end
jComp.repaint;
% Other Java components are highlighted by a transparent red-
% border panel, placed on top of them in their parent's space
elseif ~isempty(jParent)
if visible
% place the transparent red-border panel on top
jParent.add(redBorderPanel);
jParent.setComponentZOrder(redBorderPanel,0);
else
jParent.remove(redBorderPanel);
end
jParent.repaint;
end
end % idx flash loop
Categories: Figure window, GUI, Java, Medium risk of breaking in future versions
Article printed from Undocumented Matlab: https://undocumentedmatlab.com
URL to article: https://undocumentedmatlab.com/articles/figure-toolbar-customizations
URLs in this post:
[1] described: http://undocumentedmatlab.com/blog/figure-toolbar-components/
[2] uiinspect: http://www.mathworks.com/matlabcentral/fileexchange/17935
[3] FindJObj utility: http://www.mathworks.com/matlabcentral/fileexchange/14317
[4] Image: http://undocumentedmatlab.com/images/findjobj.png
[5] Figure toolbar components : https://undocumentedmatlab.com/articles/figure-toolbar-components
[6] Customizing figure toolbar background : https://undocumentedmatlab.com/articles/customizing-figure-toolbar-background
[7] Figure window customizations : https://undocumentedmatlab.com/articles/figure-window-customizations
[8] Adding a search box to figure toolbar : https://undocumentedmatlab.com/articles/adding-a-search-box-to-figure-toolbar
[9] Customizing the standard figure toolbar, menubar : https://undocumentedmatlab.com/articles/customizing-standard-figure-toolbar-menubar
[10] uicontrol side-effect: removing figure toolbar : https://undocumentedmatlab.com/articles/uicontrol-side-effect-removing-figure-toolbar
[11] : http://java.sun.com/docs/books/tutorial/uiswing/components/toolbar.html#more
Click here to print.
Copyright © Yair Altman - Undocumented Matlab. All rights reserved.
24 Comments To "Figure toolbar customizations"
#1 Comment By Jason McMains On September 2, 2009 @ 09:52
@Yair,
There are 3 features of this toolbar window that really interest me:
1) only the close button is shown in the top bar, not the minimize or restore button, does this mean it is possible to modify that top bar as well? (add and remove buttons there, or change callbacks)
2) when the toolbar is undocked, the figure name does not show up in the windows start bar. Does this mean we could have other “child” figures, that dont show up in the start bar?
3) the tool bar window stays on top of the figure, but not on top of any others.
I’m really interested to hear your thoughts
Jason
#2 Comment By Tim On September 24, 2009 @ 06:43
Any idea how to make changes to the default figure toolbar that apply for *all* figures, including new ones that have yet to be created?
#3 Comment By Yair Altman On September 24, 2009 @ 08:29
@Jason – the top (detached) toolbar is customizable just like a docked toolbar. I’m afraid I don’t know how to modify Matlab windows to have similar decoration effects (no resize/minimize buttons), nor how to create other child windows. If and when I learn more, I’ll post updates here.
@Tim – place your customization code in figureToolbarCreateFcn.m. This is an internal Matlab function (in the %matlabroot%\toolbox\matlab\graphics\ folder) that is called whenever the default figure toolbar is created. Depending on your Matlab release version, you may need to update a different internal function.
#4 Comment By Stupidfrog On November 10, 2012 @ 09:51
Excellent!
However, if I have correctly understood, modifying figureToolbarCreateFcn.m is no longer possible in recent versions (I am using 2012a). Is there a new workaround?
Thanks
#5 Comment By Tim On September 24, 2009 @ 11:08
Yair: Fantastic, that worked like a charm. I’ve used it to add a ‘Protect figure’ toggle to the standard toolbar. Here’s the code. Edit the file %matlabroot%\toolbox\matlab\graphics\figureToolbarCreateFcn.m and go to the line before this one:
and add the following code:
Thanks again!
#6 Comment By Jason McMains On October 19, 2009 @ 09:42
Yair,
I’ve just run across one more question on this topic, and I cant seem to find an answer in the java forums. Do you know of a way to make the toolbar float by default, I would expect some command along the lines of setFloating, but it doesnt exist.
Thanks again
Jason
#7 Comment By Yair Altman On October 19, 2009 @ 10:43
Jason – odd as this may sound, it appears that the default Swing JToolBar object (from which Matlab’s MJToolBar inherits) does not enable setting the ‘Floating’ property programmatically. See here, for example: [11]
Matlab’s MJToolBar extends JToolBar with a getter method (isFloating()), but not with a corresponding setter method. Doing the following throws no exception/error, but also has no visible effect:
#8 Comment By Jason McMains On October 23, 2009 @ 04:24
Yair,
Weird, it seems like something relatively fundamental that was left out. Thanks for checking on it though!
Jason
#9 Comment By Daniel On October 28, 2009 @ 21:37
Yair
I can’t find the AncestorAddedCallback. I’ve inspected the MJToolbar object with the uiinspect utility and it seems to be not accesible. am i doing something wrong?
Thanks
Daniel
#10 Comment By Yair Altman On October 29, 2009 @ 00:24
Daniel – which Matlab release and platform are you using? Also, what commands do you use to get the toolbar object and its callbacks? – The following should work:
#11 Comment By Daniel On October 29, 2009 @ 14:26
Yair
i`ve just resolved my problem.
jToolbar = handle(jToolbar, ‘CallbackProperties’); is missing in the example on the top of the page.
Thanks for the new example.
Daniel
#12 Comment By Yair Altman On October 29, 2009 @ 15:35
oops… my bad – sometimes a simple handle(obj) is enough to expose the callbacks, but sometimes not, so it is always advisable to use handle(obj,’CallbackProperties’).
Even MathWorks programmers encounter this limitation, as attested by the following internal comment within the built-in javacomponent.m function:
Anyway, I fixed the post accordingly.
#13 Comment By Kris On January 28, 2010 @ 11:36
Yair:
I was curious if you might know how to create a vertically oriented figure toolbar. I am not wise in the ways of java, and thus, I am not certain about how to go about relocating the figure toolbar from the top of the figure window to, say, the left hand side of the figure window. I am interested in designing a GUI, such that the user may elect to place my customized toolbar icons and associated functions somewhere else in the figure window besides the top.
I appreciate any feedback you might have. Thanks.
-Kris
#14 Comment By Yair Altman On January 28, 2010 @ 12:52
@Kris – interesting question! Standard Java JToolBars (which the Matlab toolbar extends) allow floating toolbars to be manually dragged and pinned to the sides, but apparently the Matlab extension prevents it.
Here is how to programmatically place the toolbar on the bottom:
Unfortunately, when we try to use the same method for placing the toolbar on the left/right sides, the figure gets “frozen” and unresponsive. Perhaps someone can enlighten us what needs to be changed in the following code:
Yair
#15 Pingback By Modifying default toolbar/menubar actions | Undocumented Matlab On June 2, 2010 @ 07:11
[…] Examples of toolbar customizations were presented in past articles (here and here). A future article will explain how to customize menu […]
#16 Comment By JP On April 27, 2011 @ 13:57
Yair,
thanks for this, it is extremely helpful. Playing around with the jToolbar properties, I was wondering if there is a way to make the toolbar width less than the width of the window? I can adjust the height using preferred size, but adjusting the width does nothing.
Also, when adding matlab toolbars, it adds the dock button to the right of the toolbar. Is there a way to remove that. It shows up in the list of jToolbar components, but removing it does not seem to work.
Thanks,
JP
#17 Comment By Yair Altman On April 27, 2011 @ 14:28
@JP – I don’t know of an easy way to reduce the toolbar’s width. Why do you wish to do this?
To remove the docking icon, simply use the fully-documented DockControls property of the figure:
#18 Comment By JP On April 27, 2011 @ 14:38
I have a GUI that has an axes that fills the bottom left 1/4 of the window. I would like the toolbar to be directly above the axes, but not spill over into the content on the right half of the window.
#19 Comment By Aris On December 12, 2011 @ 11:07
@JP
How exactly do you adjust the height?
Did you by any chance find a solution for the width?
Thanks!
#20 Comment By Yasser On August 29, 2013 @ 02:35
Hi Yair,
I am trying to build a GUI on Matlab 2012b that has a main toolbar with only three Icons, and other optional tool bars.
the point is, i need the main toolbar width to be only for the three icons, and when i select one of the optional toolbars it should come to right side of the main toolbar NOT under it.
Thanks
Yasser
#21 Comment By Yair Altman On August 29, 2013 @ 02:38
@Yasser – you would need to play around at the Java level. It is possible, but there is no two-liner solution I’m afraid. Send me an offline email if you’d like my consulting help in this.
#22 Comment By Daniel On September 29, 2013 @ 07:19
Hi all,
would there be posibility to doch a toolbar on the left side (or the right side) of a gui?
Thanks for all your help!
Daniel.
#23 Comment By Collin On July 2, 2016 @ 18:30
Yair,
With a
MJToolBar
‘s setMorePopupEnabled set to true, non-native matlab components added to the toolbar do not seem to respond when activated from the popup (I guess a jMenu derivative?) that opens when the toolbar is not large enough for all its components. Am I missing something?Collin
#24 Comment By Yair Altman On July 2, 2016 @ 22:22
@Collin – MorePopup is a Matlab extension to the standard Swing
JToolBar
so it is not surprising that it does not behave well with non-Matlab components. If you want me to research the issue to see whether a solution can be found, then email me for a short consulting.